Skip to content

Challenge Make Object Properties Private

Quincy Larson edited this page Aug 20, 2016 · 1 revision

Challenge 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 you just remove the keyword this from the object property or method declaration and declare it with var so that it is private outside its scope.

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

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

};

Another example:

var Cake = function() {

  var loot = 2;
  // Getter to know how much loot you have
  this.getLoot = function() {
    return loot;
  };

  // Setter to change the ammount of loot
  this.setLoot = function(num){
    loot = num;
  };
Clone this wiki locally