* chore(learn): audit basic algorithm scripting * chore(learn): audit basic data structures * chore(learn): audit basic javascript * chore(learn): audit debugging * chore(learn): audit es6 * chore(learn): audit functional programming * chore(learn): audit intermidate algorithms * chore(learn): audit js projects * chore(learn): audit object oriented programming * chore(learn): audit regex * fix(learn): remove stray . * fix(learn): string to code * fix(learn): missed some * fix(learn): clarify strings Based on Randy's feedback, clarifies string instances where quotes were removed in favour of back ticks. * fix: apply suggestions - thanks Randy! :) Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: non-suggestion comments * chore(learn): remove comments from codes Removes the comments from the description and instruction code blocks to ensure that all relevant information is translatable. * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: revert crowdin fix * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * chore: change voice * fix: Christopher Nolan * fix: expressions would evaluate * fix: will -> would * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: to work to push * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
2.9 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7db1367417b2b2512b87 | Add Methods After Inheritance | 1 | 301315 | add-methods-after-inheritance |
--description--
A constructor function that inherits its prototype
object from a supertype constructor function can still have its own methods in addition to inherited methods.
For example, Bird
is a constructor that inherits its prototype
from Animal
:
function Animal() { }
Animal.prototype.eat = function() {
console.log("nom nom nom");
};
function Bird() { }
Bird.prototype = Object.create(Animal.prototype);
Bird.prototype.constructor = Bird;
In addition to what is inherited from Animal
, you want to add behavior that is unique to Bird
objects. Here, Bird
will get a fly()
function. Functions are added to Bird's
prototype
the same way as any constructor function:
Bird.prototype.fly = function() {
console.log("I'm flying!");
};
Now instances of Bird
will have both eat()
and fly()
methods:
let duck = new Bird();
duck.eat();
duck.fly();
duck.eat()
would display the string nom nom nom
in the console, and duck.fly()
would display the string I'm flying!
.
--instructions--
Add all necessary code so the Dog
object inherits from Animal
and the Dog
's prototype
constructor is set to Dog
. Then add a bark()
method to the Dog
object so that beagle
can both eat()
and bark()
. The bark()
method should print Woof!
to the console.
--hints--
Animal
should not respond to the bark()
method.
assert(typeof Animal.prototype.bark == 'undefined');
Dog
should inherit the eat()
method from Animal
.
assert(typeof Dog.prototype.eat == 'function');
Dog
should have the bark()
method as an own
property.
assert(Dog.prototype.hasOwnProperty('bark'));
beagle
should be an instanceof
Animal
.
assert(beagle instanceof Animal);
The constructor for beagle
should be set to Dog
.
assert(beagle.constructor === Dog);
beagle.eat()
should log the string nom nom nom
console.log = function (msg) {
throw msg;
};
assert.throws(() => beagle.eat(), 'nom nom nom');
beagle.bark()
should log the string Woof!
console.log = function (msg) {
throw msg;
};
assert.throws(() => beagle.bark(), 'Woof!');
--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();