An example can be seen on this JSFiddle
I'm creating an instance, changing one of the functions of that instance, and then calling that function within a different function in that instance. I'm having difficulty accessing local and instance variables inside the updated function.
Code
var MyObj = function (name) {
var that = this;
MyObj.myObjs = (MyObj.myObjs || []),
this.objName = name,
this.objIdx = MyObj.myObjs.length;
MyObj.myObjs.push(this.objName);
this.doOnSetName = function () {};
this.setName = function (name) {
that.doOnNameSet();
that.objName = name;
MyObj.myObjs[that.objIdx] = name;
}
}
var obj1 = new MyObj("obj1");
//obj1.doOnNameSet = function() { alert("objName: "+this.objName) };
//obj1.setName("obj1");
var obj2 = new MyObj("obj2");
obj2.doOnNameSet = function () {
$("#console").append("Old objName: " + this.name
+ "<br />New objName: " + name + "<br />")
};
obj2.setName("obj2 - changed");
$("#console ").append("Objects: <br />*" + MyObj.myObjs.join(", <br />*"));
Actual Outcome
Old objName: undefined
New objName: result
Objects:
*obj1,
*obj2 - changed
Desired Outcome
Old objName: obj2
New objName: obj2 - changed
Objects:
*obj1,
*obj2 - changed