JS Base Variable Declaration Promotion

Source: Internet
Author: User

Promotion of variable declarations

This is a unique feature of JS, other languages do not have this feature. Some programmers are quite disgusted with this feature.

Let's go ahead and change the value of the variable, and then define the variable, because JS has a mechanism called the elevation of the variable declaration,

JavaScript's variable declarations have a hoisting mechanism, and the JavaScript engine, when executed, promotes the declaration of all variables to the front of the current scope .

So now the program will have seen a row in the program to define the variable before execution, so it will be promoted to the beginning of the program to run

123456789 var v = ‘hello‘;(function () {console.log (v); var v = ' World '}) ()
The result is undefined
123456789 var v = ‘hello‘;if (true) {Console.log (v); var v = ' World '}

The output is "hello", which means that JavaScript is not block-scoped . a function is the only structure in JavaScript that has its own scope.

Because JavaScript is a dynamic language, its variables do not have a fixed type, their storage size will vary with initialization and assignment, so the definition of its variable is not like the traditional static language, and its definition does not seem to matter.

Declaring elevation

Declarations within the current scope are promoted to the front of the scope, including declarations of variables and functions

123456 (function(){  vara = "1";  varf = function(){};  varb = "2";  varc = "3";})();

The declaration of the variable A,F,B,C is promoted to the front of the function scope, similar to the following:

1234567 ( function () {    var  A,F,B,C;    a = "1"    f = function () {};    b = "2"    c = "3"

Note that the function expression is not promoted, which is also the difference between a function expression and a function declaration. Look further at the difference between the two:

123456789 ( function () {    //var f1,function F2 () {};//hoisting, implicitly promoted declaration      f1 (); //REFERENCEERROR:F1 is not defined    f2 ();     var  f1 = function () {};    function  f2 () {}

The function declaration F2 is promoted in the above code, so it is no problem to call F2 in front. Although the variable F1 is also promoted,

However, the F1 promoted value is undefined, and its true initial value is given at execution to the function expression. So only the declaration is promoted.

Do not write Var

ABC = 123;

Console.log (ABC);//123

When I defined ABC, I did not write Var, and the program did not make an error, indicating that the ABC variable had been successfully defined.

Don't write var defines a global variable that is not controlled by scope

In JavaScript, a name (name) enters the scope (scope) in four ways, with the following order of precedence:
1, language built-in: All scopes have this and arguments keywords
2, formal parameters: function parameters are valid in the scope of the function
3, Function declaration: Shape as function foo () {}
4, Variable declaration: shape as Var bar;

The precedence of the name declaration is shown above, i.e. if the name of a variable is the same as the name of the function, then the name of the function overrides the name of the variable,

Regardless of their order in the code. However, the initialization of a name is done in the order in which it is written in the code, and is not affected by the precedence above. Look at the code:

123456789 (function(){    varfoo;    console.log(typeoffoo); //function        functionfoo(){}    foo = "foo";    console.log(typeoffoo); //string})();

If there is more than one variable with the same name in the form parameter, the last parameter with the same name overrides the other parameter with the same name, even though the last parameter with the same name is undefined.

There are exceptions to the first name resolution precedence, such as the ability to override the language's built-in name arguments.

Named function expressions

You can specify a name for a function expression like a function declaration, but this does not make the function expression a function declaration. The name of the named function expression does not enter the namespace and is not promoted.

12345 f();//TypeError: f is not a functionfoo();//ReferenceError: foo is not definedvarf = function foo(){console.log(typeoffoo);};f();//functionfoo();//ReferenceError: foo is not defined

The name of a named function expression is valid only within the scope of the function.

  

JS Base Variable Declaration Promotion

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.