The keyword "VAR" in JavaScript uses Cheung solution

Source: Internet
Author: User
Tags constructor eval variables variable window
The javascript| keyword does not remember when to look at the JScript grammar tutorial, which says that when declaring a variable, it ignores varThe keyword is completely legal. It was also because of the thought that JavaScript was loosely-typed language, so Var might really be a device. 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:

No.1

<script language= "JavaScript" >
var var00 = 0;
document.write (var00 + ' <br> '); var var01 = 1;
function foo ()
{
document.write (VAR01);
var Var01 = 1;
}
Foo ();
</script>


No.2

<script language= "JavaScript" >
var00 = 0;
document.write (var00 + ' <br> '); Var01 = 1;
function foo ()
{
document.write (VAR01);
Var01 = 1;
}
Foo ();
</script>


No.3

<script language= "JavaScript" >
var00 = 0;
document.write (var00 + ' <br> '); Var01 = 1;
function foo ()
{
document.write (VAR01);
var Var01 = 1;
}
Foo ();
</script>


The implementation results of these three examples are:

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: $$$ It is also a valid variable name, faint.

<script type= "Text/javascript" >
<!--
This writes JS the statement block better some:
-->
</script>

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.

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:
function Test (Global)
{
// ...
}
Then create the instance: var test = new Test (this); 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, otherwise 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 tasks:
• Create 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 invoked 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 means 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.