Make Object Properties Private : Clueless

Make Object Properties Private : Clueless
0

#1

So far I’ve been going along doing ok through the course but this waypoint leave’s me a bit clueless.

This is all I could come up with. Can someone help me with this waypoint?

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

  var gear;// Only change code below this line.
this.getGear = function(change){
  gear += change;
};
  this.setGear = function(){
    gear -= 5;
  };
};

var myCar = new Car();

var myBike = new Bike();

#2

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.


#3

Usually, functions that start with get don’t accept arguments. Their job is only to return a value stored in some variable.

Then, those that start with set change the value of some variable, and accepts an argument.


#4

The getGear() function should just return the value of gear. And the setGear(newGear) function shout set the variable gear to the new number. Remember to use the prefix this. before the local private property gear.


#5

I’m left wondering what should be inside the function this.setGear function.


#6

Thanks for your help. I finally figured it out.


#7

Yeah, it’s a little confusing the first time. It’s a different way to think about objects and properties. If you get into object oriented programming, you’ll see this a lot more.


#8

In ES5 you can hidden methods like this:

var Car = function() {
...
var engine = function() { return 'V8'; }
...
}

In ES6 ( Ecmascript 6) you can use WeakMap or Symbols to hidden variables and methods.
It’s very recommended use WeakMap.

const _private = new WeakMap();

class Car {
  constructor(speed=10) {
    /* only typecheck. */
    if(!(typeof speed === 'number' && speed >= 0)){
      throw new TypeError('Wrong input to class.');
    }
    
    /* props and methods to hidden. */
    const secrets = {
      speed,
      /* method in private because
       * it's inside of WeakMap.
       */
      warning: () => {
        console.log('be careful!');
      }
    };
    
    /* put speed in WeakMap. */
    _private.set(this, secrets);
  }

  /* you can't set speed, only get. */
  get speed() {
     const { speed } = _private.get(this);
     return speed;
  }

  accelerate(change) {
    /* only typecheck. */
    if(!(typeof change === 'number')){
      throw new TypeError('Wrong type to change.')
    }
    
    const state = _private.get(this);
    state.speed = this.speed + change;
    _private.set(this, state);
    
    if(state.speed > 80) {
      /* call a method in private. */
      state.warning();
    }
  }
  
  decelerate() {
    this.accelerate(-5);
  }
}

source: https://curiosity-driven.org/private-properties-in-javascript#weakmaps