Fixed typo (#34848)
Original wording: getHachedEggCount New wording: getHatchedEggCountpull/35109/head
parent
ddc8839e67
commit
e8b7b2eb6f
|
@ -11,7 +11,7 @@ In the previous challenge, <code>bird</code> had a public property <code>name</c
|
|||
Therefore, any part of your code can easily change the name of <code>bird</code> to any value. Think about things like passwords and bank accounts being easily changeable by any part of your codebase. That could cause a lot of issues.
|
||||
The simplest way to make properties private is by creating a variable within the constructor function. This changes the scope of that variable to be within the constructor function versus available globally. This way, the property can only be accessed and changed by methods also within the constructor function.
|
||||
<blockquote>function Bird() {<br> let hatchedEgg = 10; // private property<br><br> this.getHatchedEggCount = function() { // publicly available method that a bird object can use<br> return hatchedEgg;<br> };<br>}<br>let ducky = new Bird();<br>ducky.getHatchedEggCount(); // returns 10</blockquote>
|
||||
Here <code>getHachedEggCount</code> is a privileged method, because it has access to the private variable <code>hatchedEgg</code>. This is possible because <code>hatchedEgg</code> is declared in the same context as <code>getHachedEggCount</code>. In JavaScript, a function always has access to the context in which it was created. This is called <code>closure</code>.
|
||||
Here <code>getHatchedEggCount</code> is a privileged method, because it has access to the private variable <code>hatchedEgg</code>. This is possible because <code>hatchedEgg</code> is declared in the same context as <code>getHatchedEggCount</code>. In JavaScript, a function always has access to the context in which it was created. This is called <code>closure</code>.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
|
Loading…
Reference in New Issue