freeCodeCamp Challenge Guide: Use Closure to Protect Properties Within an Object from Being Modified Externally

Use Closure to Protect Properties Within an Object from Being Modified Externally


Problem Explanation

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.


Solutions

Solution 1 (Click to Show/Hide)
function Bird() {
  let weight = 15;

  this.getWeight = function() {
    return weight;
  };
}
Solution 1 (Click to Show/Hide)

In ES6 syntax we can make the function a bit less verbose:

function Bird() {
  let weight = 15;
  this.getWeight = () => weight;  
}
29 Likes

Hi,
My code seems to run properly but it doesn’t pass the test. I have looked at everything but I can’t pass the test for myBike.getGear(); despite it clearly showing that gear =4 when myBike.setGear(4);
What am I doing wrong?

var Bike = function() {
  var gear=0;
  
  this.setGear = function(addGear){
    gear += addGear;   
  };
  this.getGear = function(){
    return "Gear "+ gear;    
  };

};
var myBike = new Bike();
myBike.setGear(8);
3 Likes

dotkok when you call myBike.setGear() you are actually doing sum.
try calling at the end of your code myBike.setGear(4); then myBike.setGear(3);. You will see the result is 7.
The problem is in how you use your gear variable. Take a look at this line of code. gear += addGear;
This dose not allow the gear variable to reset to it’s initial value (0) after each call by the method.

Solution:

gear = addGear;

Hope it helped you ;).

13 Likes

Thank you so much for your reply.
I have tried your suggestion but I still do not seem to pass the test.(see pic below)

Here is the code with your correction.
var Bike = function() {

  var gear=0;
  
  
  this.setGear = function(addGear){
    gear = addGear;   
  };
  this.getGear = function(){
    return "Gear "+ gear; 
    
  };

};

var myBike = new Bike();
myBike.setGear(5);
myBike.setGear(3);
2 Likes

The reason that is not working is because you are returning a string together with the answer in the statement - return "Gear " + gear;

Try removing the “Gear” and leave only the variable gear and it should work.

9 Likes

Thank you so much!! I combined both of your corrections and it worked! Thanks!! @leizifei @vicarmocanu

2 Likes

var Bike = function() {
var gear = 10;
this.setGear = function(x){
gear = x;
};
this.getGear = function(){
return gear;
};
// Only change code below this line.

};

myBike.setGear(4);

7 Likes

This is the code that worked for me. I think it’s important that you don’t define gear.
var Bike = function() {

// Only change code below this line.
var gear;
this.setGear = function(x) {
gear = x;
};
this.getGear = function() {
return gear;

};

};

var myBike = new Bike();

14 Likes
var Bike = function() {
  var gear = 10;

  // Only change code below this line.
  this.getGear = function() {
    return gear;
  };
  
  this.setGear = function (change){
    gear = change;
};
};

var myCar = new Car();

var myBike = new Bike();
9 Likes

Nice I was doing the same thing!!! Thanks

is location of this.setgear and this.getgear matters?

  var Bike = function() {
  // Only change code below this line.
  
  var gear;
  
  this.getGear= function(){
    return gear;
  };
  
  this.setGear=function(setgear){
    gear = setgear;
  };
  };

is above code different than below one

var Bike = function() {

  // Only change code below this line.
  
  var gear;

  this.setGear=function(setgear){
    gear = setgear;
  };
  
  this.getGear= function(){
    return gear;
  };  
};

both above code passes test.
even I put “var gear” at the end of object it passes test, why?

2 Likes

The location of the methods do not matter. the order in which the methods are invoked matter which in this case is setGear first and then getGear.

Just declaring var gear means that its value is initially undefined. when setGear is called, gear is set to whatever was passed in as an argument. so it does not matter in this case if gear was set to an initial value or not

1 Like

This also worked:

var Bike = function() {

// Only change code below this line.
var gear = 0;

this.getGear = function() {
return gear;
};

this.setGear = function(change) {
gear = change;

};

};

var myCar = new Car();

var myBike = new Bike();
myBike.setGear(4);

2 Likes

Hello campers,this is how I approached it:

var Car = function() {
// this is a private variable
var speed = 10;

// these are public methods
this.accelerate = function(change) {
speed += change;
};

this.decelerate = function() {
speed -= 5;
};

this.getSpeed = function() {
return speed;
};
};

var Bike = function() {

// Only change code below this line.
var gear = 5;//private variable

//public methods
this.setGear = function(change) {
gear = change;
};

this.getGear = function() {
return gear;
};

};

var myCar = new Car();
myCar.getSpeed();

var myBike = new Bike();
myBike.getGear();

1 Like

var Car = function() {
// this is a private variable
var speed = 10;

// these are public methods
this.accelerate = function(change) {
speed += change;
};

this.decelerate = function() {
speed -= 5;
};

this.getSpeed = function() {
return speed;
};
};

var Bike = function() {

// Only change code below this line.
var gear = 0;
// public methods.

this.getGear=function (change){
change= gear;
} ;

this.setGear=function(){
return gear ;
};
};

var myCar = new Car();

var myBike = new Bike();
myBike.setGear(1);
myBike.setGear(3);
myBike.setGear(4);

Hi there, i am having issue with this challenge, i cant seem to figure this out

AGAIN!!! Another example of NOT teaching us what we need to know to solve the problems.
I understand I can google stuff. But some of the exercises are very specific.

You CANNOT solve this problem without knowing that you need Num.

this.setLoot = function(num){
loot = num;
};

But you don’t learn that this is even a parameter that need to be passed until you look in the Hints.

PLEASE, PLEASE, PLEASE. I want to learn this stuff through practice, not by getting the answers.

16 Likes

I don’t get any error message with this code. Could someone point out what I am doing wrong? Thanks!

var Bike = function() {
var gear;
this.getGear = function(num){
num = gear;
};
this.setGear = function(){
return gear;
};
// Only change code below this line.

};

var myCar = new Car();

var myBike = new Bike();
myBike.getGear(4);

EDIT: Figured it out.

It says set.Gear should be called - not get.Gear.

THIS WORKS:
var Bike = function() {
var gear = 0;

this.getGear = function() {
return gear;
};

this.setGear = function(num) {
gear = num;

};

};

var myCar = new Car();

var myBike = new Bike();
myBike.setGear(4);

This worked for me

5 Likes