Scope and scope chain-javascript object-oriented advanced

Source: Internet
Author: User

# scope and scope chain #

# 1. Scope # #

Definition: When a function is declared, the scope of the current function is determined, so-called scopes are the scope of the action (site)

Global scope:

A. window is the top-level object, which is the end point of all functions, variables, objects,
B. So a function or variable that is defined at the global scope is a property or method that belongs to the Window object.
C. Its scope is global and can be invoked anywhere.



Local scope:

A. When a function is declared, the scope inside the function is the local scope,
B. Variables and declarative functions declared through Var inside a function are private variables or functions of the function.
C. In general, a private variable or function inside a function cannot be accessed outside the function


Scope Chain:

A. When a variable is called inside a function, it is found in the current scope whether the variable is declared
B. If it is not declared, it will go to its previous scope to find
C. Until the global scope is found, or not if it is not define



The relationship between scope and execution context:

var a = ten, B = 10;
function fn (x) {
var a = 100,c = 200;
function bar (x) {
var a = 1000,d = 4000;
}
Bar (100)
Bar (200)
}
FN (10)

Global scope: Save a = 10,b = 10 in the context ring
fn Scope: Within the context ring save a = 100,x = ten, c = 200
Bar Scope: Inside the context ring save a = 1000,d =4000,x = 100 (First call) 200 (second call)

Scope and scope chain-javascript object-oriented advanced

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.