Confused about how increment
can change value
and that changed value
is accessible to getValue
, but not to a property of the same object.
Does this have to do with the fact that increment and getValue methods are inner functions of the anonymous function that value is defined in?
var myObject = function() {
var value = 0;
return {
increment: function(inc){
value += typeof inc === 'number' ? inc : 1;
},
getValue: function(){
return value;
},
value: value,
};
}();
console.log(myObject.getValue()); // 0
myObject.increment(2);
console.log(myObject.getValue()); // 2
console.log(myObject.value); // 0
myObject.increment(2);
console.log(myObject.getValue()); // 4