I have a javascript function that has a number of methods and variables and returns an object literal I am using somewhat like a class, i.e.
var myObject = function {
var somevars;
var someMethod = function(someInput) { };
return {
methodA:function(inputs) {
// calls someMethod, using somevars and inputs
},
methodB:function(inputs) {
// calls someMethod, using somevars and inputs
}
};
}
In javascript it is common to create a variable called "that" or "self" or some such thing that stores the value of "this" at creation time so that in future calls one can use it to point to the objects own self because "this" may be pointing to something else. This is useful if, e.g. methodA gets used as a click handler.
How can I create a "that" variable in the object literal I return?
Is there a good way to initialize a "that" variable in an object literal, or is something more round about necessary?
Update
Yes, the object I want to be able to reference is the object literal itself.
Yes, methodA and methodB might also call each other and refer to other members of the returned object literal. That is why I care about "this".
I create a number of instances of this object via "new". Call one of them X. Then I want to use X.methodA as a mouse event handler, e.g. I have jquery code that attaches mouseup on a dom element to X.methodA. But that looks like $("#domElementId").mousemove(X.methodA) But when mousemove is called, the "this" variable used in methodA no longer points to X as I understand. Feel free to correct me if I am wrong. Specifically, calls within method A to, say, method B will fail because they have do be done using this.methodB and "this" points to the global object.