On the execution of JS

Source: Internet
Author: User

Scope:
Scope Chain:
Child scopes can access the variables and methods of the parent scope. The parent set has no access to the variables and methods of the subset scope
Variable declaration elevation:
JS execution will be divided into two stages: (JS pre-compilation and top-down execution)
JS pre-compile: JS declaration when the Var declaration and function refers to the top of the script (also can be understood as the current scope), the assignment statement will not be promoted but in situ wait for assignment

Assignment statements do not increase:

Console.log (a) var a=3; var a=function () {    console.log (1//undefined

function experience is promoted (and precedence is higher than variable declaration):

var a=3; Console.log (a) function A () {    console.log (1

If the inside of the function is lifted to the top of the function:

var a=function () {        var b=3        function B () {            Console.log (  'JJ')            }        Console.log (b)    } ()

The declaration of a variable * (undefined) does not overwrite anything:

function Test () {    console.log (2);    } var test;        Console.log (test);     var test = function () {    console.log (1);    } Console.log (test);

Obey the nearest principle:

var a=4    var b=function () {        a=3         Console.log (A)    } ()  //3

When a function is called inside another function, its scope is the same as the scope of the original function (if you want to pass the current scope of the variable into the required parameters):

function X () {    console.log (a)}var y=function () {    var a=2     x ()} ()  //  a is not defined
Everyone must be very strange about this, okay. Synchronous execution top-down
(function y () { var a=2 x (A)}) () function X (a) { console.log (a)} // actually equals function y () { var a=2 x (a)}y () function X (a) { console.log (a)}

 

In strict mode (use strict)

When you write variables, you must declare them or you will get an error.

" Use Strict " a=3  //a is not defined
" Use Strict " (function X () {Console.log (1)}) () // self-calling functions cannot be used in strict mode
" Use Strict " x=function () {Console.log (1)} ()  //1   x is not defined  the declaration executes when the function is written, but it also causes an error.

2 new declarations (let const) in ES6

var defines a variable that has no block concept, can be accessed across blocks, and cannot be accessed across functions.
Let defines a variable that can be accessed only within a block scope, not across blocks, or across functions.

{Let a=4} function x () {Console.log (a)}x ()   // a is not defined

Const is used to define constants, which must be initialized (that is, they must be assigned or not), and can only be accessed in the block scope and cannot be modified.

Const Aconsole.log (a)   // Missing initializer in const declaration

1. Re-definition is not allowed in ES6

Let a=4; var a=2//  Identifier ' A ' has already been declared
var a=4 leta=3console.log (a)  //  Identifier ' A ' have already been Declared
A=4; Let a=3; Console.log (a)  //a are not defined

2. Variable does not allow declaration of elevation

Console.log (a) Let    a//a are not defined

3.let when defining variables, you can keep values in block-level scope

var oli=document.queryselectorall (' #box li ')for (let i=0;i<4;i++) {Oli[i].onclick =function() {    console.log (i)   //0,1,2,3}}

On the execution of JS

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.