If i have:
var obj={
a:function(){obj.b();},
b:function(){this.a();}
};
is there any difference in calling object methods with “this” or referring directly to the object itself because it is on the same scope as the method?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Depends
thiscan take many forms, making it unpredictable:In a normal function call, like
funcName(),thisis the global object. In browsers, it’s thewindowobject.In a normal function call where the function uses “use strict”,
thisisundefined.For a function used as a constructor, like
var instance = new ConstructorFunction(),thiswill refer to the instance object created from that constructor.For object literals,
thisis the immediate object literal enclosing the function.When called by
call(context,args...)orapply(context,[args...]),thisis whatevercontextis.