What is the difference between these?
var a = 13;
this.b = 21;
document.write(a);
document.write(b);
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.
For global code (code that is not part of any function), they are almost equivalent, both at the end create a property on the global object.
The difference is that
a, which has been declared with thevarstatement, the Variable Instantiation process will use the global object as the Variable Object (1), and it will define that property as non-deleteable on it, e.g.:Then,
bsince thethisvalue in global code, points to the global object itself, will be also a global property, but this one can be deleted:Don’t try the first snippet in Firebug, since the Firebug’s console runs the code internally with
eval, and in this execution context the variable instantiation process behaves differently, you can try it here.(1) The Variable Object (VO) is an object that is used by the variable instantiation process to define the identifiers of FunctionDeclarations, identifiers declared with the
varstatements, and identifiers of function formal parameters, in the different execution contexts, all those identifiers are bound as properties of the VO, the Scope chain is formed of a list of VO’s.For global code, the VO is the global object itself, that’s why
aends being a property of it. For function code, the VO (also known as the Activation Object for FunctionCode), is a new object is created behind the scenes when you invoke a function, and that is what creates a new lexical scope, in short I’ll talk about functions.Both
aandthis.bcan be resolved simply as byaandbbecause the first object in the scope chain, is again the global object.Also, I think is work knowing that the Variable Instantiation process takes place before than the code execution, for example:
These differences may be trivial but I think is worth knowing them.
Now, if the code snippet you posted is within a function, it is completely different.
The
aidentifier, declared with thevarstatement in your example will be a local variable, available only to the lexical scope of the function (and any nested functions).Keep in mind that in JavaScript blocks don’t introduce a new scope, only functions do, and to declare a variable in that scope, you should always use
var.The
this.bidentifier will become a property bound to the object that is referred by thethisvalue, but… What isthis???.The
thisvalue in JavaScript is implicitly set when you call a function, it is determined by how do you invoke it:When you use the
newoperator, thethisvalue inside the function, will point to a newly created object, e.g.:When you call a function that is member of an object, the
thisvalue inside that function will point to the base object, e.g.:When you invoke a function without any base object, the
thisvalue will refer to the global object:The
thisvalue can be set explicitly, when you invoke a function usingcallorapply: