freeCodeCamp/curriculum/challenges/russian/02-javascript-algorithms-an.../object-oriented-programming/add-methods-after-inheritan...

4.2 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7db1367417b2b2512b87 Add Methods After Inheritance 1 Добавить методы после наследования

Description

Функция-конструктор, которая наследует свой объект- prototype от функции-конструктора supertype может по-прежнему иметь свои собственные методы в дополнение к унаследованным методам. Например, Bird - это конструктор, который наследует свой prototype от Animal :
функция 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() . Функции добавляются к prototype Bird's же, как и любая функция конструктора:
Bird.prototype.fly = function () {
console.log («Я летаю!»);
};
Теперь экземпляры Bird будут иметь методы eat() и fly() :
let duck = new Bird ();
duck.eat (); // печатает "nom nom nom"
duck.fly (); // печатает «Я лечу!»

Instructions

Добавьте все необходимые символы , поэтому Dog объект наследует от Animal и в Dog's prototype конструктора установлена Собаке. Затем добавьте метод bark() к объекту Dog чтобы beagle мог как eat() и bark() . Метод bark() должен печатать «Woof!» на консоль.

Tests

tests:
  - text: <code>Animal</code> не должно отвечать методу <code>bark()</code> .
    testString: 'assert(typeof Animal.prototype.bark == "undefined", "<code>Animal</code> should not respond to the <code>bark()</code> method.");'
  - text: <code>Dog</code> должна наследовать метод <code>eat()</code> от <code>Animal</code> .
    testString: 'assert(typeof Dog.prototype.eat == "function", "<code>Dog</code> should inherit the <code>eat()</code> method from <code>Animal</code>.");'
  - text: <code>Dog</code> должна иметь метод <code>bark()</code> как <code>own</code> свойство.
    testString: 'assert(Dog.prototype.hasOwnProperty("bark"), "<code>Dog</code> should have the <code>bark()</code> method as an <code>own</code> property.");'
  - text: <code>beagle</code> должен быть <code>instanceof</code> <code>Animal</code> .
    testString: 'assert(beagle instanceof Animal, "<code>beagle</code> should be an <code>instanceof</code> <code>Animal</code>.");'
  - text: Конструктор для <code>beagle</code> должен быть установлен на <code>Dog</code> .
    testString: 'assert(beagle.constructor === Dog, "The constructor for <code>beagle</code> should be set to <code>Dog</code>.");'

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

// solution required