Welcome to amateur hour, starring me. I was sending a parameter to my 'new object' call named 'id.' turns out that the 'id' I thought I was getting (the variable name stored in the window object associative array) was actually the one I was passing as a parameter.
This raises a question, though. Say I have the following:
Code:
var myObjInstance = new myCustomObj(param,...);
Is there a way to make a 'this' statement inside 'myCustomObj' object return the id/name of the variable it was assigned to without sending it through as a parameter? In other words, can I dynamically tell what the window object thinks 'this' is named from inside the object without first passing it?
I think maybe an explanation of how the 'this' keyword works will help you.
The 'this' keyword always refers to the context in which the function is being executed unless there is no valid context. In that case it uses the window object as the context.
So if you do this:
Code:
document.onclick = myObj.myMethod;
Any 'this' keyword in the function myMethod would refer to document because that is what is executing the function. To be accurate, in the example above we're actually making a copy of myMethod and assigning it as the onclick event handler.
To have the 'this' keyword refer to your custom object, you need to execute the function within the context of your custom object.
Now document is executing a generic function that tells your object to execute its method. Since the method is being called from your object, the 'this' keyword will now refer to the custom object.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.