如何防止直接在js/ts类中更新属性?

如果两个数组包含相同数量的项目,那么您可以使用普通的 for 循环来访问具有相同索引的每个项目,如下所示 -


let home_team = ["A", "B", "C"];

let away_team = ["D", "E", "F"];


for(let i=0; i<home_team.length; i++){

  console.log(`${home_team[i]} VS ${away_team[i]}`);

}

展开片段

或者,您也可以在 for 循环中使用多个计数器,如下所示(尽管您现在不需要这个 -


let home_team = ["A", "B", "C"];

let away_team = ["D", "E", "F"];


for(let i=0,j=2; i<home_team.length; i++, j--){

  console.log(`${home_team[i]} VS ${away_team[j]}`);

}


子衿沉夜
浏览 87回答 1
1回答

慕婉清6462132

请将所有私有变量放入构造函数中。class Animal {&nbsp; constructor() {&nbsp; &nbsp; let privateAttribute = 'default';&nbsp; &nbsp; this.setPrivateAttribute = newValue => {&nbsp; &nbsp; &nbsp; privateAttribute = newValue&nbsp; &nbsp; }&nbsp; &nbsp; this.getPrivateAttribute = () => privateAttribute;&nbsp; }}let newAnimal = new Animal()// get variable valuenewAnimal.getPrivateAttribute()// Set new ValuenewAnimal.setPrivateAttribute('New Value')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript