freeCodeCamp/guide/chinese/certifications/javascript-algorithms-and-d.../object-oriented-programming/iterate-over-all-properties/index.md

797 B

title localeTitle
Iterate Over All Properties 迭代所有属性

迭代所有属性

方法

该方法是使用for-in-loop迭代对象中的每个属性。然后在循环内部检查属性是own-property还是prototype ,并将其放在ownProps[]数组或prototypeProps[]数组中。请记住push属性push送到beagle对象而不是Dog对象以传递所有测试用例。

function Dog(name) { 
  this.name = name; 
 } 
 
 Dog.prototype.numLegs = 4; 
 
 let beagle = new Dog("Snoopy"); 
 
 let ownProps = []; 
 let prototypeProps = []; 
 
 // Add your code below this line 
 for (let property in beagle) { 
  if(Dog.hasOwnProperty(property)) { 
    ownProps.push(property) 
  } 
  else { 
    prototypeProps.push(property) 
  } 
 }