Waypoint Make Object Properties Private - GJSmith3rd/FreeCodeCamp-BootCamp GitHub Wiki

Contact me

Gilbert Joseph Smith III

@gjsmith3rd

Github | FreeCodeCamp | CodePen | LinkedIn | Blog/Site | E-Mail

Make Object Properties Private

Objects have their own attributes, called properties, and their own functions, called methods.

You can use the this keyword to reference public properties and methods of the current objects. However, when You need to create private ones, so they are not accessible from the outside of the object.

For that, you just remove the keyword this from the object property or method declaration.

var Bike = function() {
  speed = 100; // private
  function addUnit(value) { // private
    return value + "KM/H";
  }

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

};