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

2.0 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7daf367417b2b2512b7e 了解构造函数属性 1 301327 understand-the-constructor-property

--description--

在上一个挑战中创建的实例对象duckbeagle都有一个特殊的constructor属性:

let duck = new Bird();
let beagle = new Dog();

console.log(duck.constructor === Bird);  //prints true
console.log(beagle.constructor === Dog);  //prints true

需要注意到的是这个constructor属性是对创建这个实例的构造函数的一个引用。 constructor属性存在的一个优势是,我们可以通过检查这个属性来找出它是一个什么样的对象。下面是一个例子,来看看是怎么使用的:

function joinBirdFraternity(candidate) {
  if (candidate.constructor === Bird) {
    return true;
  } else {
    return false;
  }
}

注意:
由于constructor属性可以被重写(在下面两节挑战中将会遇到),所以使用instanceof方法来检查对象的类型会更好。

--instructions--

写一个joinDogFraternity函数,传入一个candidate参数并使用constructor属性来判断传入的 candidate 是不是Dog创建的对象实例,如果是,就返回true,否则返回false

--hints--

joinDogFraternity应该被定义为一个函数。

assert(typeof joinDogFraternity === 'function');

如果candidateDog的一个对象实例,则joinDogFraternity函数应该返回true

assert(joinDogFraternity(new Dog('')) === true);

joinDogFraternity中应该用到constructor属性。

assert(/\.constructor/.test(code) && !/instanceof/.test(code));

--seed--

--seed-contents--

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

// Only change code below this line
function joinDogFraternity(candidate) {

}

--solutions--

function Dog(name) {
  this.name = name;
}
function joinDogFraternity(candidate) {
  return candidate.constructor === Dog;
}