如果两个数组包含相同数量的项目,那么您可以使用普通的 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]}`);}
1 回答
慕婉清6462132
TA贡献1804条经验 获得超2个赞
请将所有私有变量放入构造函数中。
class Animal {
constructor() {
let privateAttribute = 'default';
this.setPrivateAttribute = newValue => {
privateAttribute = newValue
}
this.getPrivateAttribute = () => privateAttribute;
}
}
let newAnimal = new Animal()
// get variable value
newAnimal.getPrivateAttribute()
// Set new Value
newAnimal.setPrivateAttribute('New Value')
添加回答
举报
0/150
提交
取消