In my application,I use following codes:
(function(){
var app={
toHomePage:function(){
var that=this;
var handler={
xx:function(){
//now I have to call the clear method.
// 1) app.clear();
// 2) that.clear()
}
};
},
resize:function(){},
clear:function(){}
}
})();
I want to know in the handler method, use app or that?
Note that in a function,
thisis set entirely by how you call the function. If you will only ever call the function using:then within the function
thiswill referenceapp. However, if someone does:then
thiswithin the function will initially be undefined, so it will be set to the global object or, in strict mode, toundefined. The same forapplyandcall, wherethiscan be set to anything.So likely better to just use
app, since the identifier is within a closure and therefore unlikely to change its name. BTW, this is a common dilemma.Edit
To explain the listener case: