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.5 KiB
Raw Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7db1367417b2b2512b87 继承后添加方法 1 301315 add-methods-after-inheritance

--description--

父类继承其原型对象的构造函数除了继承的方法之外,还可以有自己的方法。

请看举例:Bird是一个构造函数,它继承了Animal构造函数的原型

function Animal() { }
Animal.prototype.eat = function() {
  console.log("nom nom nom");
};
function Bird() { }
Bird.prototype = Object.create(Animal.prototype);
Bird.prototype.constructor = Bird;

除了从Animal构造函数继承的行为之外,还需要给Bird对象添加它独有的行为。这里,我们给Bird对象添加一个fly()函数。函数会以一种与其他构造函数相同的方式添加到Bird原型中:

Bird.prototype.fly = function() {
  console.log("I'm flying!");
};

现在Bird的实例中就有了eat()fly()这两个方法:

let duck = new Bird();
duck.eat(); // prints "nom nom nom"
duck.fly(); // prints "I'm flying!"

--instructions--

添加必要的代码,使得Dog对象继承Animal构造函数,并且把Dog 原型上的 constructor 属性设置为 Dog。然后给Dog对象添加一个bark()方法,这样的话,beagle将同时拥有eat()bark()这两个方法。bark()方法中应该输出 "Woof!" 到控制台。

--hints--

Animal应该没有bark()方法。

assert(typeof Animal.prototype.bark == 'undefined');

Dog应该继承了Animal构造函数的eat()方法。

assert(typeof Dog.prototype.eat == 'function');

Dog应该有一个bark()方法作为自身属性。

assert(Dog.prototype.hasOwnProperty('bark'));

beagle应该是Animal的一个instanceof

assert(beagle instanceof Animal);

beagle的 constructor 属性应该被设置为Dog

assert(beagle.constructor === Dog);

--seed--

--seed-contents--

function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };

function Dog() { }

// Only change code below this line




// Only change code above this line

let beagle = new Dog();

--solutions--

function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };

function Dog() { }
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function () {
  console.log('Woof!');
};
let beagle = new Dog();

beagle.eat();
beagle.bark();