Files
.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
add-methods-after-inheritance.chinese.md
change-the-prototype-to-a-new-object.chinese.md
create-a-basic-javascript-object.chinese.md
create-a-method-on-an-object.chinese.md
define-a-constructor-function.chinese.md
extend-constructors-to-receive-arguments.chinese.md
inherit-behaviors-from-a-supertype.chinese.md
iterate-over-all-properties.chinese.md
make-code-more-reusable-with-the-this-keyword.chinese.md
override-inherited-methods.chinese.md
remember-to-set-the-constructor-property-when-changing-the-prototype.chinese.md
reset-an-inherited-constructor-property.chinese.md
set-the-childs-prototype-to-an-instance-of-the-parent.chinese.md
understand-own-properties.chinese.md
understand-the-constructor-property.chinese.md
understand-the-immediately-invoked-function-expression-iife.chinese.md
understand-the-prototype-chain.chinese.md
understand-where-an-objects-prototype-comes-from.chinese.md
use-a-constructor-to-create-objects.chinese.md
use-a-mixin-to-add-common-behavior-between-unrelated-objects.chinese.md
use-an-iife-to-create-a-module.chinese.md
use-closure-to-protect-properties-within-an-object-from-being-modified-externally.chinese.md
use-dot-notation-to-access-the-properties-of-an-object.chinese.md
use-inheritance-so-you-dont-repeat-yourself.chinese.md
use-prototype-properties-to-reduce-duplicate-code.chinese.md
verify-an-objects-constructor-with-instanceof.chinese.md
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
english
portuguese
russian
spanish
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
lib.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
cypress-install.js
cypress.json
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
jest.config.js
lerna.json
lighthouserc.js
package-lock.json
package.json
sample.env
2020-08-04 12:45:28 +05:30

3.2 KiB
Raw Blame History

id, title, challengeType, forumTopicId, localeTitle
id title challengeType forumTopicId localeTitle
587d7db1367417b2b2512b87 Add Methods After Inheritance 1 301315 继承后添加方法

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!" 到控制台。

Tests

tests:
  - text: <code>Animal</code>应该没有<code>bark()</code>方法。
    testString: assert(typeof Animal.prototype.bark == "undefined");
  - text: <code>Dog</code>应该继承了<code>Animal</code>构造函数的<code>eat()</code>方法。
    testString: assert(typeof Dog.prototype.eat == "function");
  - text: <code>Dog</code>应该有一个<code>bark()</code>方法作为<code>自身</code>属性。
    testString: assert(Dog.prototype.hasOwnProperty('bark'));
  - text: <code>beagle</code>应该是<code>Animal</code>的一个<code>instanceof</code>。
    testString: assert(beagle instanceof Animal);
  - text: <code>beagle</code>的 constructor 属性应该被设置为<code>Dog</code>。
    testString: assert(beagle.constructor === Dog);

Challenge Seed

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

function Dog() { }

// Add your code below this line




// Add your code above this line

let beagle = new Dog();

beagle.eat(); // Should print "nom nom nom"
beagle.bark(); // Should print "Woof!"

Solution

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();