Implicit variable declaration and VAR variable Declaration In JScript

Source: Internet
Author: User
I have never noticed the difference between the implicit variable Declaration In JScript and the VaR explicit variable declaration. Although some problems have been encountered in the declaration, it has not caused much attention. I wrote several functions today and checked the reference manual. I felt it was necessary to change the traditional implicit declaration practices to avoid unnecessary troubles. The Manual describes the "variable Declaration" as follows:

The first appearance of a variable in the script is in the Declaration. The variable is set to memory when it is used for the first time, so that it can be referenced in the script later. Declare variables before using them. AvailableVaRKeyword.

VaR Count; // A single statement.

VaR Count, amount, level; // Multiple declarations declared with a single var keyword.

VaR Count =   0 , Amount =   100 ; // Declare and initialize variables in a statement.

IfVaRThe statement does not contain initialization variables. The variables automatically take the JScript value.Undefined. Although it is not safe, it is ignored in the declaration statement.VaRThe keyword is a valid JScript syntax. In this case, the JScript interpreter gives visibility to the global scope of the variable. When a variable is declared at the process level, it cannot be used for a global range; in this case, the variable DeclarationRequiredUseVaRKeyword.

Here, the scope of "process-level declaration variables" is clearly not described, maybe it is a problem of Chinese and English translation. Let's look at an example:

Function Foo1 (){

For (I = 0 ; I < 10 ; I ++ );

}


Function Foo2 (){

Alert (I );

}


Foo1 ();

Foo2 ();

Guess what output is there? Or error?

The fact is that a dialog box is displayed: 10. However, if you call foo2 () or change foo1 () to the following:
FunctionFoo1 (){

VaRI;

For(I=0; I<10; I++);

}

But it does not show anything, but a script error prompt: "I is undefined ".

so we can see that if a variable is implicitly declared in the function, the variable will become a global variable ! Like the variable I in the original foo1 () function, the value 10 after the loop is retained and displayed in foo2. However, if I is declared using VAR display in foo1 (), it is a function-level local variable () I cannot be found.
it seems that although the system allows implicit variable declaration during JScript writing, explicit variable declaration should be performed, especially for habitual cyclic variables, to ensure its scope of use.

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.