What is the difference between the two?
var a = 13; this.b = 21; document.write(a); document.write(b);
For global code (code that is not part of any function) they are almost equivalent, and finally create a property for the global object.
The difference is that a , declared using the var statement, is a process
a
var
Then b , since the this value in the global code points to the global object itself, will also be a global property, but this can be removed:
b
this
this.b = 21; delete b; // true typeof b; // "undefined"
Do not try to execute the first fragment in Firebug, since the Firebug console internally executes the eval code, and in this execution context, the process of creating an instance of a variable behaves differently, you can try it.
eval
(1) A Variable Object (VO) is an object that is used by the variable instantiation process to define FunctionDeclarations identifiers, identifiers declared using var statements, and function identifiers formal parameters in different contexts, all these identifiers are related as VO properties, chain Scope is formed from the VO list.
For global code, VO is a global object, so a ends as a property of this object. For function code, VO (also known as FunctionCode) is a new object created behind the scenes when the function is called, and this is what creates the new lexical scope, in short, I will talk about functions.
Both a and this.b can simply be a and b , because the first object in the chain area is again a global object.
this.b
In addition, I think this is a job, knowing that the "Instance Variable" process occurs earlier than the code execution, for example:
alert(a); // undefined, it exists but has no value assigned to it yet alert(b); // ReferenceError is thrown var a = 13; this.b = 21;
These differences may be trivial, but I think it's worth knowing them.
Now, if the piece of code that you sent is inside the function, it is completely different.
The identifier a declared using the var operator in your example will be a local variable, accessible only for the lexical scope of the function (and any nested functions).
Keep in mind that JavaScript blocks do not introduce a new scope, only functions are executed and declare a variable in this scope, you should always use var .
this.b will be the property associated with the object referenced by this , but ... What is this ???.
This value in JavaScript is implicitly set when the function is called, it is determined by how you call it:
When you use the new operator, the this value inside the function points to the newly created object, for example:
new
function Test() { this.foo = "bar"; } var obj = new Test(); // a new object with a `foo` property
When you call a function that is a member of an object, the this value inside this function points to the underlying object, for example:
var obj = { foo: function () { return this == obj; } }; obj.foo(); // true
When you call a function without any underlying object, this value will refer to the global object:
function test() { return this == window; } test(); // true
This value can be explicitly set when calling a function with or:
function test() { alert(this); } test.call("hello world!"); // alerts "hello world!"
To understand briefly, if you use them in a function, then -
this.a; //will create a public property var b; //will create a member variable
eg. here is the Student class in javascript
var Student = function() { // Member variable var studentId; // Public property this.Name = ""; }
for more details - see Object Oriented Programming Using JavaScript