In the JScript syntax tutorial, it is completely legal to ignore the var keyword when declaring a variable. However, facts often prove that the result is unreliable. Let's take a look at the results of the following examples to find out the problem:
The execution results of these three examples are:
The Code is as follows:
Results # region Results
No.1
0
Undefined
No. 2
0
1
No. 3
0
Undefined
# Endregion
Originally, JavaScript variables also have scopes, but they are very general and divided into global variables and function variables. In the second example, we get 0 and 1 because all the variables are global variables, and the statement block defines two variables in total. The global variables outside the first and third functions do show that the var keyword is irrelevant. The var keyword in the function is critical. It indicates that the second var01 is a variable in the function. Therefore, before initializing var01, the output is 'undefined.
Does the global var01 block in the function? We know that in C/C ++, we can use: to access global variables. Can JavaScript be used? Here, we only need to understand what global variables are. The original global variables are all attributes dynamically added to the Window object instance window, so we only need to use: document. write (window. var01); you can get its value 1. In this context, this in the function is also the window instance to which it points. We can also write the reference as this. var01.
By the way, when looking at the JScript tutorial, it says the variable can only be in the format of [a-zA-Z _] + [a-zA-Z0-9, however, '$' can also be used as a variable name character and can also be used at the beginning, for example, $1234, or even $ is also a valid variable name, faint.
I want to know if it is. when calling a function, the program execution sequence first checks whether there is a keyword var in the internal variable of the function. then, different scopes and variable values are assigned to different variables based on the check results. as I can see in these three functions, the var01 variable is after the output statement.
The Code is 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 above example, this has never referred to Test, but it has always been a WScript instance.
If we write a statement: var test = new Test (); then this in Test refers to an instance of Test. If Global variables are used in this instance, lostinet provides a method.
However, the simplest way is to pass global into an object and define Test as follows:
The Code is as follows:
Function Test (global)
{
//...
}
Then create an instance: var test = new Test (this); then you can use global objects and attributes in the Test instance.
Is that true? In the new instance, this refers to the instance; otherwise, it refers to WScript? If this is the case, will the situation be different if I change the script engine? Is this a standard?
New constructor [(arguments)];
The new operator executes the following tasks:
· Create an object without members.
· Call the constructor for that object and pass a pointer to the newly created object as the this pointer.
· Then, the constructor initializes the object based on the parameters passed to it.
Note that even if new is not called in the current scope, new may be called in its parent scope. Therefore, "in the new instance, this refers to the instance, otherwise, WScript? ", The first half is correct, and the last half is not necessarily.