You need to use that . this gets overridden in new areas, including the anonymous function you are binding.
Consider this code:
function say(x){ alert(x); } var myscope = { label : "myscope (toplevel)", func1 : function() { say("enter func1"); var label = "inner func1"; say(" this.label: " + this.label); say(" label: " + label); }, func2 : function() { var label = "inner func2"; say("enter func2"); this.func1(); } }; myscope.func2();
If you run this, it will behave nicely, and the links to this.xxx will succeed as you would like. However, if you add func3 as follows:
func3 : function() { setTimeout( function(){ var label = "anonymous func"; say("enter anon func"); this.func1(); }, 2); }
... it will not work as you might imagine. Since an anonymous function is defined without an explicit scope, this inside it refers to a top-level object, window . And there is no func1 as a child of the window (without a top-level object named func1 ).
For the same reason, this in your anon function, which you use when calling bind() , will not work. In this case, you can directly refer to the "namespace" object, or you can use closure (some call it the " that trick)):
bindMethodsToNavigation: function() { var that = this; $("#subnavigation a").bind('click', function(event) { var chosenLink = event.target; that.removeClassFromSubNav(); that.loadPartial(chosenLink); that.addChosenClassToNav(chosenLink); return false; }); }
this in the field of bindMethodsToNavigation refers to newAndImproved . this in an anonymous function will refer to window . Using closure allows you to reference what you want. (editorial comment: I personally find that name pretty and completely useless. I like to use shortened names, in your case there might be something like nai to refer to newAndImproved )
By the way, you can also do this:
clickHandler : function(event) { var chosenLink = event.target; this.removeClassFromSubNav(); this.loadPartial(chosenLink); this.addChosenClassToNav(chosenLink); return false; }, bindMethodsToNavigation: function() { $("#subnavigation a").bind('click', clickHandler); },
... and if they are both members of the newAndImproved object, then this inside clickHandler will be resolved to the newAndImproved object. The key point here is that clickHandler not an anonymous top-level function; it belongs to newAndImproved , and when this used inside it, it resolves accordingly. Moreover, you can simply refuse to use this inside clickHandler ; this is not true, but it is not necessary. Some people like to add them for emphasis to those who can read the code later.
EDIT
Another point about this is the decision to use anonymous functions or named functions as event handlers ... Anon functions are so convenient and easy, and everyone uses them. What I find is that in Firebug or IE Javascript Debugger, when I look at the stack trace, I get a list of anonymous functions, and it's hard to understand what a function chain is. Especially with AJAX and async handlers that fire in response to user interface events and so on. To provide additional visibility during debugging, I sometimes use named functions (we put them in namespaces), and in a callstack several named fns will be shown along with interspersed several anon fns. Sometimes it helps. However, this is a small thing.