Bird
构造函数定义了两个属性:name
和numLegs
:
```js
function Bird(name) {
this.name = name;
this.numLegs = 2;
}
let duck = new Bird("Donald");
let canary = new Bird("Tweety");
```
name
和numLegs
被叫做自身
属性,因为他们是直接在实例对象上定义的。这就意味着duck
和canary
这两个对象分别拥有这些属性的独立副本。
事实上,Bird
的这些实例都将拥有这些属性的独立副本。
以下的代码将duck
里面所有的自身
属性都存到一个叫ownProps
的数组里面:
```js
let ownProps = [];
for (let property in duck) {
if(duck.hasOwnProperty(property)) {
ownProps.push(property);
}
}
console.log(ownProps); // prints [ "name", "numLegs" ]
```
canary
对象里面的自身
属性添加到ownProps
数组里面。
ownProps
应该包含'numLegs'
和'name'
两个属性的值。"
testString: assert(ownProps.indexOf('name') !== -1 && ownProps.indexOf('numLegs') !== -1);
- text: 在不使用内置方法Object.keys()
的情况下完成这个挑战。
testString: assert(!/Object(\.keys|\[(['"`])keys\2\])/.test(code));
- text: You should solve this challenge without hardcoding the ownProps
array.
testString: assert(!/\[\s*(?:'|")(?:name|numLegs)|(?:push|concat)\(\s*(?:'|")(?:name|numLegs)/.test(code));
```