Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* 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>
2021-01-12 19:31:00 -07:00

1.9 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7daf367417b2b2512b7d 迭代所有属性 1 301320 iterate-over-all-properties

--description--

现在你已经了解了两种属性: 自身属性和原型属性。自身属性是直接在对象上定义的。而原型属性是定义在prototype上的:

function Bird(name) {
  this.name = name;  // 自身属性
}

Bird.prototype.numLegs = 2; // 原型属性

let duck = new Bird("Donald");

这个示例会告诉你如何将duck自身属性和原型属性分别添加到ownProps数组和prototypeProps数组里面:

let ownProps = [];
let prototypeProps = [];

for (let property in duck) {
  if(duck.hasOwnProperty(property)) {
    ownProps.push(property);
  } else {
    prototypeProps.push(property);
  }
}

console.log(ownProps); // prints ["name"]
console.log(prototypeProps); // prints ["numLegs"]

--instructions--

beagle的自身属性都添加到ownProps数组里面去。将Dog的所有原型属性添加到prototypeProps数组中。

--hints--

这个ownProps数组应该包含'name'这个值。

assert(ownProps.indexOf('name') !== -1);

这个prototypeProps数组应该包含'numLegs'这个值。

assert(prototypeProps.indexOf('numLegs') !== -1);

在不使用内置方法Object.keys()的情况下完成这个挑战。

assert(!/\Object.keys/.test(code));

--seed--

--seed-contents--

function Dog(name) {
  this.name = name;
}

Dog.prototype.numLegs = 4;

let beagle = new Dog("Snoopy");

let ownProps = [];
let prototypeProps = [];

// Only change code below this line

--solutions--

function Dog(name) {
  this.name = name;
}

Dog.prototype.numLegs = 4;

let beagle = new Dog("Snoopy");

let ownProps = [];
let prototypeProps = [];
for (let prop in beagle) {
  if (beagle.hasOwnProperty(prop)) {
    ownProps.push(prop);
  } else {
    prototypeProps.push(prop);
  }
}