--description--
Since numLegs
will probably have the same value for all instances of Bird
, you essentially have a duplicated variable numLegs
inside each Bird
instance.
This may not be an issue when there are only two instances, but imagine if there are millions of instances. That would be a lot of duplicated variables.
A better way is to use the prototype
of Bird
. Properties in the prototype
are shared among ALL instances of Bird
. Here's how to add numLegs
to the Bird prototype
:
Bird.prototype.numLegs = 2;
Now all instances of Bird
have the numLegs
property.
console.log(duck.numLegs);
console.log(canary.numLegs);
Since all instances automatically have the properties on the prototype
, think of a prototype
as a "recipe" for creating objects. Note that the prototype
for duck
and canary
is part of the Bird
constructor as Bird.prototype
.
--instructions--
Add a numLegs
property to the prototype
of Dog
--hints--
beagle
should have a numLegs
property.
assert(beagle.numLegs !== undefined);
beagle.numLegs
should be a number.
assert(typeof beagle.numLegs === 'number');
numLegs
should be a prototype
property not an own property.
assert(beagle.hasOwnProperty('numLegs') === false);
--seed--
--seed-contents--
function Dog(name) {
this.name = name;
}
// Only change code above this line
let beagle = new Dog("Snoopy");
--solutions--
function Dog (name) {
this.name = name;
}
Dog.prototype.numLegs = 4;
let beagle = new Dog("Snoopy");