好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

js中for...in语句的使用注意

说明

1、index索引为字符串数字(注意非数字),不能直接进行几何操作。

2、遍历顺序可能不是基于实际数组的内部顺序(可能是基于随机顺序)。

3、一般不建议使用for...in来遍历数组。

实例

const temp = {name: "temp"};
function Apple() {
    this.color = 'red';
}
 
Apple.prototype = temp;
 
const obj = new Apple();
 
for (const prop in obj) {
    console.log(`obj.${ prop } = ${ obj[prop] }`);
}
 
// obj.color = red
// obj.name = temp

以上就是js中for...in语句的使用注意,希望对大家有所帮助。 更多js学习指路: js教程

推荐操作环境:windows7系统、jquery3.2.1版本,DELL G3电脑。

查看更多关于js中for...in语句的使用注意的详细内容...

  阅读:14次