3.6 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 587d7db1367417b2b2512b87 | Methoden nach der Vererbung hinzufügen | 1 | 301315 | add-methods-after-inheritance |
--description--
Eine Konstruktorfunktion, die ihr prototype-Objekt von einer Supertyp-Konstruktorfunktion erbt, kann zusätzlich zu den geerbten Methoden noch eigene Methoden besitzen.
Zum Beispiel ist Bird ein Konstruktor, der seinen prototype von Animal erbt:
function Animal() { }
Animal.prototype.eat = function() {
console.log("nom nom nom");
};
function Bird() { }
Bird.prototype = Object.create(Animal.prototype);
Bird.prototype.constructor = Bird;
Zusätzlich zu dem, was von Animal geerbt wird, möchtest du ein Verhalten hinzufügen, das nur für Bird-Objekte gilt. Hier bekommt Bird eine Funktion fly(). Funktionen werden dem Bird's-prototype auf die gleiche Weise hinzugefügt wie jede Konstruktorfunktion:
Bird.prototype.fly = function() {
console.log("I'm flying!");
};
Jetzt haben Instanzen von Bird sowohl die Methoden eat() als auch fly():
let duck = new Bird();
duck.eat();
duck.fly();
duck.eat() würde den String nom nom nom in der Konsole anzeigen und duck.fly() würde den String I'm flying! anzeigen.
--instructions--
Füge den notwendigen Code hinzu, damit das Dog-. Objekt von Animal erbt und der prototype-Konstruktor von Dog auf Dog gesetzt wird. Dann füge dem Dog-Objekt eine Methode bark() hinzu, damit beagle sowohl eat() als auch bark() kann. Die Methode bark() sollte Woof! auf der Konsole ausgeben.
--hints--
Animal sollte nicht auf die Methode bark() reagieren.
assert(typeof Animal.prototype.bark == 'undefined');
Dog sollte die Methode eat() von Animal erben.
assert(typeof Dog.prototype.eat == 'function');
Der Dog-Prototyp sollte eine Methode bark() besitzen.
assert('bark' in Dog.prototype);
beagle sollte eine Instanz (instanceof) von Animal sein.
assert(beagle instanceof Animal);
Der Konstruktor für beagle sollte auf Dog gesetzt werden.
assert(beagle.constructor === Dog);
beagle.eat() sollte den String nom nom nom in der Konsole ausgeben.
capture();
beagle.eat();
uncapture();
assert(logOutput == 'nom nom nom');
beagle.bark() sollte den String Woof! in der Konsole ausgeben.
capture();
beagle.bark();
uncapture();
assert(logOutput == 'Woof!');
--seed--
--before-user-code--
var logOutput = "";
var originalConsole = console
function capture() {
var nativeLog = console.log;
console.log = function (message) {
logOutput = message;
if(nativeLog.apply) {
nativeLog.apply(originalConsole, arguments);
} else {
var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
nativeLog(nativeMsg);
}
};
}
function uncapture() {
console.log = originalConsole.log;
}
capture();
--after-user-code--
uncapture();
(function() { return logOutput || "console.log never called"; })();
--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();