* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2.0 KiB
2.0 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 587d7dae367417b2b2512b7b | 了解自己的属性 | 1 | 301326 | understand-own-properties |
--description--
请看下面的实例,Bird构造函数定义了两个属性:name和numLegs:
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的数组里面:
let ownProps = [];
for (let property in duck) {
if(duck.hasOwnProperty(property)) {
ownProps.push(property);
}
}
console.log(ownProps); // prints [ "name", "numLegs" ]
--instructions--
将canary对象里面的自身属性添加到ownProps数组里面。
--hints--
ownProps应该包含'numLegs'和'name'两个属性的值。
assert(ownProps.indexOf('name') !== -1 && ownProps.indexOf('numLegs') !== -1);
在不使用内置方法Object.keys()的情况下完成这个挑战。
assert(!/Object(\.keys|\[(['"`])keys\2\])/.test(code));
You should solve this challenge without hardcoding the ownProps array.
assert(
!/\[\s*(?:'|")(?:name|numLegs)|(?:push|concat)\(\s*(?:'|")(?:name|numLegs)/.test(
code
)
);
--seed--
--seed-contents--
function Bird(name) {
this.name = name;
this.numLegs = 2;
}
let canary = new Bird("Tweety");
let ownProps = [];
// Only change code below this line
--solutions--
function Bird(name) {
this.name = name;
this.numLegs = 2;
}
let canary = new Bird("Tweety");
function getOwnProps (obj) {
const props = [];
for (let prop in obj) {
if (obj.hasOwnProperty(prop)) {
props.push(prop);
}
}
return props;
}
const ownProps = getOwnProps(canary);