The keyword "VAR" in JavaScript is shared using detailed

Source: Internet
Author: User
Tags constructor eval
JScript's Grammar tutorial says it is perfectly legal to ignore the VAR keyword when declaring a variable.   But it often turns out that the consequences of being taken for granted are unreliable.

Look at the results of the following examples to know the problem:
The implementation results of these three examples are:

Copy Code code as follows:


results#region Results


No.1


0


undefined


No.2


0


1


No.3


0


undefined


#endregion


The original JavaScript variable is also scoped, but it is very general, it is divided into global variables and function variables. In the second example, we get 0 and 1 because all the variables are global variables, and that statement block defines two variables altogether. The first third, the global variable, does indicate that the VAR keyword has no relation. The var keyword within the function is critical, stating that the second Var01 is a variable within the function, so the output of the Var01 before initialization is ' undefined '.
So the function inside is to block out the var01 of the global? We know that you can use the following:: To access global variables, then javascript? In fact, we just need to understand what the global variable is, so we can get it. The original global variable is a property that is dynamically added to the instance window of the Window object, so we simply use: document.write (WINDOW.VAR01) in the function, and we can fetch the value 1. Also in this context, this in the function also points to the window instance, and we can write the reference as: This.var01.
By the way, when you re looking at the JScript tutorial, it says that the variable can only be [a-za-z_]+[a-za-z0-9_]* format, but ' $ ' can also be used as a variable-name character, and it can also be applied at the beginning, such as: $1234, even more: $$$ is also a valid variable name, faint.
I was wondering if it was. When a function is invoked, the sequence of execution of the program checks the function's internal variables to see if there is a keyword var. Then the different scopes and variable values are given to different variables according to the results of the inspection. Because I see in these three functions, the VAR01 variable is after the output statement.

Copy Code code as follows:


function Get_global_var (___name)


{


return eval (___name);


}


function Set_global_var (___name,___value)


{


eval (___name+ "=___value");


}


var aa=11;


Test ();


WScript.Echo (AA);//22


function Test ()


{


var aa=33;


WScript.Echo (Get_global_var ("AA"));//11


set_global_var ("AA", 22);


WScript.Echo (Get_global_var ("AA"));//22


WScript.Echo (AA);//33


}


In the example above, this has never been referred to test, and has always been an instance of WScript.
If we write a statement: var test = new Test (); This in test is an instance of test, in which the lostinet gives a method if the variable of global is to be used.
However, it is easiest to pass the global to the object, which defines test:

Copy Code code as follows:


function Test (global)


{


// ...


}


and then create the instance: var test = new Test (this), and you can use the objects and properties of global in the test instance.
is that so, in the instance of new, this refers to an instance, or does it all refer to WScript? If that's the case, would it be different if you changed the script engine? Is this standard?
New constructor[(arguments)]; The
new operator performs the following task:
• Creates an object that has no members.
• Call the constructor for that object, passing a pointer to the newly created object as the this pointer.
• The constructor then initializes the object based on the arguments passed to it.
Note that even if new is not called in the current scope, it may be called New in its parent scope, so "in the instance of new, this is an instance, otherwise it is WScript?", the first half is right, and the second half is not necessarily.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.