Make Object Properties Privates

Make Object Properties Privates
0.0 0

#1

Tell us what’s happening:
Hi guys , I’m trying solve this problem but to be honest i coul not understrand the concept
so I hope to learn the concept , then the correct solution

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() {

  // Only change code below this line.

};

var myCar = new Car();

var myBike = new Bike();

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:


#2

We can also create private properties and private methods, which aren’t accessible from outside the object.

To do this, we create the variable inside the constructor using the var keyword we’re familiar with, instead of creating it as a property of this.

When a property (or method) is created in a constructor using the var keyword (like speed in your text above) they are private properties. When a property (or method) is created using this. it is public.