JScript variable range

Source: Internet
Author: User

 

JScript has two variables: global and local. If a variable is declared outside the definition of any function, the variable is a global variable, and the value of the variable can be accessed and modified throughout the continuous range. If a variable is declared in the Function Definition

Is a local variable. This variable is created and destroyed every time you execute this function, and it cannot be accessed by anything other than this function.

Languages like C ++ also have block scopes ". Here, any pair of "{}" defines a new range. JScript does not support block range.

The name of a local variable can be the same as that of a global variable, but it is completely different and independent. Therefore, changing the value of a variable does not affect the value of another variable. In the function that declares a local variable, only this local variable makes sense.

.

VaR acentaur = "a horse with Rider,"; // global definition of acentaur.

// JScriptCodeIs omitted for the sake of conciseness.
Function antiquities () // declare a local acentaur variable in this function.
{

// JScript code, which is omitted for the sake of conciseness.
VaR acentaur = "a Centaur is probably a mounted Scythian warrior ";

// JScript code, which is omitted for the sake of conciseness.
Acentaur + = ", misreported; that is,"; // Add to local variable.

// JScript code, which is omitted for the sake of conciseness.
} // The function ends.

VaR nothinginparticipant ular = antiquities ();
Acentaur + = "as seen from a distance by a naive innocent .";

/*
In the function, the value of this variable is "a Centaur is probably a mounted Scythian warrior,
Misreported; that is, "; outside the function, the value of this variable is the rest of this sentence:
"A horse with rider, as seen from a distance by a naive innocent ."
*/
It is important to note whether the variable is declared at the beginning of its scope. Sometimes this can lead to unexpected situations.

Tweak ();
VaR anumber = 100;
Function tweak (){
VaR newthing = 0; // explicitly declare the newthing variable.

// This statement assigns undefined variables to newthing because there is a local variable named anumber.
Newthing = anumber;

// The next statement assigns the value 42 to the local anumber. ANumber = 42;
If (false ){
VaR anumber; // This statement will never be executed.
ANumber = 123; // This statement will never be executed.
} // The Condition Statement ends.

} // The Function Definition ends.
When JScript runs a function, it first looks for all the variable declarations,

VaR somevariable;
Create a variable with an undefined initial value. If a variable has a value declared,

VaR somevariable = "something ";
The variable is still initialized with an undefined value, and the declared value is replaced only when the declared row is run, if it has been declared.

JScript pre-processes the variable declaration before running the code. Therefore, it does not matter whether the declaration is in a condition block or in some other structures. JScript finds all the variables and immediately runs the code in the function. If the variable is explicitly declared in the function

If it appears on the left of the value assignment expression but does not use VaR Declaration-it is created as a global variable.

 

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.