let f = function () {
  this.a = 1;
  this.b = 2;
};
let o = new f();   // {a: 1, b: 2}
f.prototype.b = 3;
f.prototype.c = 4;
for(let i in o){
  console.log(i) //a,b,c   if(o.hasOwnProperty(i)){     console.log(i) //a,b   } }

  for...in语句使您可以遍历对象的所有属性的名称。属性列表包括通过原型链继承的所有那些属性。所以需要 hasOwnProperty 过滤

for (name in object) {
  if (object.hasOwnProperty(name)) {
    doSomething(name);
  }
}

但克隆对象时例外

for (prop in obj) {
  a[prop] = obj[prop];  // Compliant by exception
}

 

相关文章:

  • 2021-08-01
  • 2021-08-21
  • 2021-09-25
  • 2021-12-15
  • 2022-02-01
  • 2022-02-08
  • 2021-07-02
  • 2022-12-23
猜你喜欢
  • 2021-06-29
  • 2022-12-23
  • 2021-12-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案