797 B
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)
}
}