You're assigning functions to the prototype object that gets assigned to new SimpleObject
every time the constructor is called. So the second time the constructor is called, the prototype is updated, and any previous object created using the prototype starts using the new function (with the new self
value).
If you want those closures to have self
, don't put them on the prototype, put them on this
(more below).
But note that in the code you've quoted, there's no reason for the self
variable. You can use prototype functions (which is more efficient) rather than creating functions for each instance. To do that, use this
(without assigning it to self
) and define the functions outside the constructor, like this:
"use strict";
//simple object
module.exports.SimpleObject = function SimpleObject(inputName) {
this.name = inputName;
};
SimpleObject.prototype.getName = function() {
return this.name;
};
SimpleObject.prototype.nestedOverwrite = function(anObject) {
return "#1 this.name: "+this.name+" ObjectName: "+anObject.getName()+" #2 this.name: "+this.name;
};
But if you're doing something with self
that you aren't showing that means you really need it (e.g., that you can't trust the value of this
when the functions are called &mdasdh; they're being used as callbacks/event handlers, etc.), here's how you do that:
"use strict";
//simple object
module.exports.SimpleObject = function SimpleObject(inputName) {
var self = this;
self.name = inputName;
self.getName = function() {
return self.name;
};
self.nestedOverwrite = function(anObject) {
return "#1 self.name: "+self.name+" ObjectName: "+anObject.getName()+" #2 self.name: "+self.name;
};
};
Side notes:
There's no reason to
return this
at the end of a constructor function.Note that we don't want to do
self = this;
in each of the functions; the whole point of usingself
is that when the function is called, you don't want to rely onthis
having the correct value. (If yoU can rely onthis
being set, then use the prototype version of the functions.)Never catch an exception in a constructor function and just say you couldn't do the construction, because by doing that, you allow the
new Xyz
expression to complete, and the un-initialized object is returned as the result of the expression. I've removed thetry/catch
from the above for that reason.I recommend not relying on the horror that is automatic semicolon insertion. End expressions with semicolons (including when you're assigning a function to a variable or property).