Make Object Properties Private- can't solve it

Make Object Properties Private- can't solve it
0.0 0

#1

Tell us what’s happening:
I tried to copy the code above but I’m not good with OOP. Please help? My code doesn’t work.

Your code so far

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() {
  gear= this.gear;

  // Only change code below this line.

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

var myCar = new Car();

var myBike = new Bike();

myBike.setGear(3);
myBike.getGear();

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36.

Link to the challenge:


#2

I don’t think you want to modify the existing value of gear. Try setting an entirely new value instead.

Something like this should work, although it will return undefined if you call getGear() before setGear()

var Bike = function() {
  var gear;

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