Scope of JavaScript

Source: Internet
Author: User

After reading a few books some understanding and own ideas.    scopes, the scope of the variables   declarations of variables before ES6   only VAR can declare variables to belong to a scope, and also only global scope and function scope. (variables that do not have VAR declarations, are global scopes, variables declared in global scope, functions become global properties) all variables, not global scopes, or function scopes; If you declare a variable with VAR and it is in a function, that variable belongs to the function, otherwise it is a global variable.   variables, function promotion   in any scope, there is an elevation;       for a declaration, the JS engine is processed and executed.        so-called processing, the engine will first perform a global scan, the declaration of the variable (var declared variable) will be recorded, encountered function declaration (Functions keyword beginning) will also be recorded, Until the global scan is complete. The engine then starts from scratch, modifies the variable, and invokes the function.       Top-of-the-record is the act of Ascension. (If you learn the C language, you know that functions, variables are defined first, in use, otherwise will be error, but, JS Ascension, can be understood as, no matter where you define, will be promoted to the front of the use, that is, you can write the use in front of the definition)      The    engine logs declared variables, function declarations to the global scope, records what variables exist, and assigns variables to initialize, undefined, and why it is logged to the global, not the corresponding scope, mainly because, at the beginning of the scan, the global scope is scanned, It scans only one level, it does not go deep into the scan and only scans the surface. This is actually very good understanding, in addition to the promotion of the variable, there is the function of the promotion of the Declaration, the JS engine is simply to elevate it to the topmost level, defined in the global scope of the function, it scope is equivalent to two levels, JS engine will not be at this time to scan function scope.        for function scope promotion, occurs after the function is called, before execution, the same surface scan, record the variables attached to the scope of the function.         if a variable and function name is the same in a scope, the function has a higher precedence, and the identifier Fuzhengan is the function name. &nbsp es6  declaration of variables, VAR,  let, const, for Var, no change, with ES6 before and let declare variables, const declaration constants, for these two, they have a block-level scope, as long as the { }, the surrounding area, is a block-level scope. In the block-level scope, the Let,const declaration is internally visible and invisible to the outside. And it will be attached to the scope.  Also, let and const do not exist for elevation. Let and const cannot declare the same identifier repeatedly. (See ES6 related books for details).    above is the main reference to the JavaScript you do not know, a very powerful book. Welcome you to correct me, thank you.    

Scope of JavaScript

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.