Sorry, I cannot even formulate my question correctly, because my brain cannot comprehend this behavior:
var $a = (function(){
var innerVariable = "Hello from A";
this.show = function(){
console.log(innerVariable);
}
return this;
})();
var $b = (function(){
var innerVariable = "Hello from B";
this.show = function(){
console.log(innerVariable);
}
return this;
})();
$a.show(); // "Hello from B" - why $b.innerVariable overwrites $a.innerVariable?
$b.show(); // "Hello from B" - okay, expected and logical
https://jsfiddle.net/9a4cpvzg/1/
I am trying to simulate standard OOP(Java) on ES5. I would like to have these 2 "objects" with public functions.
Can anyone explain to me why $b.innerVariable
overwrites $a.innerVariable
?
EDIT:
Thanks to answer, I see that using this
is the problem here.
I just saw an internet example of simulating standard OOP in JS and they I followed it - using this
inside the functions.
My goal is to be able to define objects with public and private functions/variables. this
is not important in my case - I just saw it in internet.
Could you please suggest solution working on ES5
?