Home > Web Front-end > JS Tutorial > Detailed explanation of the use of keyword 'VAR' in JavaScript Share_Basic knowledge

Detailed explanation of the use of keyword 'VAR' in JavaScript Share_Basic knowledge

WBOY
Release: 2016-05-16 17:27:44
Original
1139 people have browsed it

You will know the problem if you look at the results of the following examples:
The execution results of these three examples are:

Copy code The code is as follows:

Results#region Results
No.1
0
undefined
No.2
0
1
No.3
0
undefined
#endregion

It turns out that JavaScript variables also have scopes, but they are very general and are divided into global variables and function variables. In the second example, 0 and 1 are obtained because all variables are global variables, and that statement block defines a total of two variables. The first and third global variables outside the function really mean that it does not matter whether the var keyword is present or not. The var keyword within the function is very critical. It indicates that the second var01 is a variable within the function, so the output before initializing var01 is naturally 'undefined'.
So is the global var01 blocked in the function? We know that in C/C you can use :: to access global variables, but can it be done in JavaScript? In fact, as long as we understand what global variables are, it will be easy. It turns out that global variables are attributes dynamically added to the window instance of the Window object, so we only need to use: document.write(window.var01); within the function to get its value 1. At the same time, in this context, this in the function is also the window instance pointed to. We can also write the reference as: this.var01.
By the way, when I re-read the JScript tutorial, it said that variables can only be in the format [a-zA-Z_] [a-zA-Z0-9_]*, but '$' can also be used as a variable name character. And it can also be used at the beginning, such as: $1234, and even: $$$ is also a legal variable name, faint.
I want to know if it is true. When calling a function, the program execution sequence will first check whether there is the keyword var in the internal variables of the function. Then, different scopes and variables will be assigned to different variables based on the check results. value. Because I see that in these three functions, the var01 variable is after the output statement.
Copy code 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 never refers to Test, but always Example of WScript.
If we write a statement: var test = new Test(); then this in Test refers to an instance of Test. In this instance, if you want to use Global variables, Lostinet gives a method.
But the easiest way is to pass global into the object and define Test like this:
Copy the code The code is as follows:

function Test(global)
{
// ...
}

Then create the instance like this: var test = new Test(this); Just Global objects and properties can be used in Test instances.
Is this the case? In the instance of new, this refers to the instance, otherwise it refers to WScript? If this is the case, will the situation be different if the script engine is changed? Is this a standard?
new constructor[(arguments)];
The new operator performs the following tasks:
·Creates an object with 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 parameters passed to it.
It should be noted that even if new is not called in the current scope, new may be called in its parent scope, so "in an instance of new, this refers to the instance, otherwise it refers to WScript?", first half The sentence is correct, but the second half of the sentence is not.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template