A brief introduction to JavaScript scopes and scope chains

Source: Internet
Author: User

Scope and scope chain Brief introduction:

As long as it is a programming language, then the scope is an inescapable problem, the following is an example of a simple introduction of JavaScript scope and scope chain.

In JavaScript, scopes are divided into two types:

1. Global scope

2. Local scope.

How to define what scopes are:

1. Variables defined outside of all functions are global in scope.

2. A variable defined within a function has a local scope, and its scope is the function in which the variable resides.

Special Note: All variables that have a direct assignment to the last definition have global scope.

The scope and scope can be contained in layers:

Global scopes can contain local scopes, and other local scopes can be nested within local scopes, and can be nested so indefinitely.

A few examples to deepen the understanding of the concept:

Example one:

<Scripttype= "Text/javascript"> varx="global variable x"; vary="global variable y"; functionMyFunction () {varx="local variable x"; varZ="local variable z"; alert (x);//Popup "local variable X"alert (y);//"global variable Y" pops up} myfunction (); alert (z);//content cannot be ejected. </Script>

In the above code, when executing the MyFunction () function, the "local variable X" and "local variable Y" can be ejected separately. However, alert (z) cannot eject content because the z variable is a local variable defined within the function and cannot be used outside the body of the function.
From the above code can have a basic understanding of local variables and global variables, in particular, if the local variables and global variables with the same name, then the local variables will overwrite the global variables, the reason is described later.
Example two:

<Scripttype= "Text/javascript"> varx="global variable x"; vary="global variable y"; functionMyFunction () {varx="local variable x"; varZ="local variable z"; M="global variable M"; alert (x);//Popup "local variable X"alert (y);//"global variable Y" pops up} myfunction (); Alert (m);//"global variable M" pops up</Script>

In the above code, when executing the MyFunction () function, the "local variable X" and "local variable Y" can be ejected separately. Alert (m) is able to eject "global variable M", although M is declared and assigned in the function body, but does not use the var keyword, so the scope of the variable m is global.
Example three:

<Scripttype= "Text/javascript"> varx="global variable x"; vary="global variable y"; functionMyFunction () {varx="local variable x"; varZ="local variable z"; M="global variable M"; alert (x);//Popup "local variable X"alert (y);//"global variable Y" pops up  functioninnerfunction () {varxx="local variable xx"; Alert (xx);//popup "local variable xx"alert (z);//popup "local variable z"} innerfunction (); Alert (XX)//cannot eject} myfunction (); Alert (m);//"global variable M" pops up</Script>

In the above code, the nesting of functions, that is, the implementation of the scope of the nesting. Running results here are not introduced, and the above principle is the same, need to specifically note that alert (XX) is not able to pop content, because the scope of the XX variable is the innerfunction () function, and in MyFunction () Neither the function nor the global function has a declaration about the XX variable.
After a few examples above, a preliminary understanding of the scope, the following to introduce the JavaScript variable lookup mechanism, you can understand why the same name of the local variable can overwrite the global variables.
With instance three as an example, the alert (z) Statement of the Innerfunction () function is first found in the scope of the innerfunction () function, and if it does not exist, it is looked up in the scope of the MyFunction () function, If there is a pop-up variable value, none continues to look up, thus forming a scope chain.
Special Note: There is no block-level scope in JavaScript, such as a For loop statement that does not form a function.

The original address is: http://www.51texiao.cn/javascriptjiaocheng/2015/0430/485.html

The original address is: http://www.softwhy.com/

A brief introduction to JavaScript scopes and scope chains

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.