Make Object Properties Private (Fucntion Clarification)

Tell us what’s happening:
I was able to pass the test, but I am not understanding how calling setgear function returns the gear value when its not defined within the setgear function. How does getvalue is called?

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 gear = 9;
  this.getGear = function(){
    return  gear;
  this.setGear = function(gears){
    gear =  gears;

var myCar = new Car();

var myBike = new Bike();

Your browser information:

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

Link to the challenge:

1 Like

Thank you for sharing the link, but I am still confused. It seems like if i change the function of set or get it does not work as expected. Is this set and get function predefined javascript function?

The short answer is that functions have access to variables declared outside of themselves (in level above) so getGear has access to variable gear and can return that value. (setGear function does not return the value of the gear but it does change it so also has access to gear.)

var outer = "I'm in outer level";

function test(){
   // you can access outer from inside test
   console.log(outer); // prints I'm in outer level
   var inner = "I'm inside this function";

console.log(inner); // fails - cannot access inner from here