I created an object like the following.
var BaseObject = function(){ var base = this; base.prop; base.setProp = function(val){ base.prop = val; } }
When I call the setProp method, I get the following.
var a = new BaseObject(); var b = new BaseObject(); a.setProp("foo"); b.setProp("bar"); console.log(a.prop); // outputs 'foo' console.log(b.prop); // outputs 'bar'
Then I created another object that inherits from BaseObject like this.
var TestObject = function(){
When I do the same, I get a result that I did not expect.
var a = new TestObject(); var b = new TestObject(); a.setProp("foo"); b.setProp("bar"); console.log(a.prop); // outputs 'bar' console.log(b.prop); // outputs 'bar'
I do not know why. I have read a lot lately about closure and prototype inheritance, and I suspect all of this is messed up. Thus, any pointers to why this particular example works the way it did would be greatly appreciated.
gargantuan
source share