Basic JS challenge: use-prototype-properties link added (#37802)
* Update use-prototype-properties-to-reduce-duplicate-code.english.md * Update use-prototype-properties-to-reduce-duplicate-code.english.md
This commit is contained in:
@ -9,7 +9,7 @@ forumTopicId: 301336
|
|||||||
<section id='description'>
|
<section id='description'>
|
||||||
Since <code>numLegs</code> will probably have the same value for all instances of <code>Bird</code>, you essentially have a duplicated variable <code>numLegs</code> inside each <code>Bird</code> instance.
|
Since <code>numLegs</code> will probably have the same value for all instances of <code>Bird</code>, you essentially have a duplicated variable <code>numLegs</code> inside each <code>Bird</code> 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.
|
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 <code>Bird’s</code> <code>prototype</code>. The <code>prototype</code> is an object that is shared among ALL instances of <code>Bird</code>. Here's how to add <code>numLegs</code> to the <code>Bird prototype</code>:
|
A better way is to use <code>Bird’s</code> <code>prototype</code>. Properties in the <code>prototype</code> are shared among ALL instances of <code>Bird</code>. Here's how to add <code>numLegs</code> to the <code>Bird prototype</code>:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
Bird.prototype.numLegs = 2;
|
Bird.prototype.numLegs = 2;
|
||||||
@ -26,6 +26,7 @@ Since all instances automatically have the properties on the <code>prototype</co
|
|||||||
Note that the <code>prototype</code> for <code>duck</code> and <code>canary</code> is part of the <code>Bird</code> constructor as <code>Bird.prototype</code>. Nearly every object in JavaScript has a <code>prototype</code> property which is part of the constructor function that created it.
|
Note that the <code>prototype</code> for <code>duck</code> and <code>canary</code> is part of the <code>Bird</code> constructor as <code>Bird.prototype</code>. Nearly every object in JavaScript has a <code>prototype</code> property which is part of the constructor function that created it.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Add a <code>numLegs</code> property to the <code>prototype</code> of <code>Dog</code>
|
Add a <code>numLegs</code> property to the <code>prototype</code> of <code>Dog</code>
|
||||||
|
Reference in New Issue
Block a user