* ES6 syntax Added ES6 syntax to challenge' solution. * Add ES6 solution to challenge An optional way to solve the challenge using ES6 arrow functions
823 B
823 B
title
title |
---|
Use Closure to Protect Properties Within an Object from Being Modified Externally |
Use Closure to Protect Properties Within an Object from Being Modified Externally
Method
Just like in the example given, rather than declaring the weight
variable with the this
keyword, the let
keyword must be used to declare it as a private variable. This way it can only be accessed inside the Bird
function. The getWeight
method must then be added inside the Bird
function to access the weight
variable.
Solution
function Bird() {
let weight = 15;
this.getWeight = function() {
return weight;
};
}
Solution 2
In ES6 syntax we can make the function a bit less verbose:
function Bird() {
let weight = 15;
this.getWeight = () => weight;
}