I'm trying hard to understand basic concepts of javascript. The code below seems to be working fine if I use only "gear += 1" in line 8 below, but I cannot understand why is this not working when I'm using "this.gear += 1". It gives result as NaN. Thank you.
(function bike(speed, tank, gear) {
var i = {};
i.speed = speed;
i.tank = tank;
i.gear = gear;
i.addgear = (function() {
// works fine with "return gear+= 1" Why not with "this"?
return this.gear += 1;
})();
console.log("mybike", i);
})(120, 12, 5);
thisto work in the first place? None of this looks class-y (including pre-ES5 JS OOP), so I'm not quite sure what to explain.addgear'sthisto bei... except that the OP is, perhaps unintentionally, assigning the result of an IIFE toaddgearrather than an anonymous function.thiswork" post... that wasn't what this question asked, not directly, nor does the answer there really help explain either what's wrong with the code supplied or how to get the desired behavior.