JS functions and the promotion of variables

Source: Internet
Author: User

JS functions and the promotion of variables

1. Scope of function:

In JS, a function is scoped to a function, not a curly brace.

var hei = 123;
if (true) {
HEI = 456;
}
Console.log (HEI);//456;

var hei = 123;
if (true) {

}
Console.log (HEI); 123

Variables outside the function can be used inside the function, and variables outside the function cannot be used inside the function (closures can be used to achieve the effect, then summarized).

(function () {var hei = 456;}) (); Self-executing functions for functions; There are three kinds:
1) (function () {}) ();
2) (function () {} ());
3) !function () {} ();

2. Increase of variables:

var a = 1;
var b = 100;
function Z () {
Console.log (b); //Why B does not output undefined, this is because there is no var b = 99; but B = 99; So there is no promotion, he is a global variable. can also be used outside of the function.
Console.log (a); Undefined
var a = 10;
Console.log (a); 10
A = 4;
b = in the same; If it becomes var b = 99, the outside of the function cannot be used the output becomes 100. The preceding output becomes undefined, because the variable is lifted.
Console.log (a); 4
Console.log (b); 99
}
Z ();
Console.log (b); 99


if (false) {
var i = 10;
}
Console.log (i); Why is undefined, also a variable promotion.

3. Function Promotion

The first thing to be clear is two points:

<1> function promotion only for function declarations

<2> function Promotion increases the function body together, which is different from the variable promotion

The following shows that the function expression cannot be promoted:

~function () {
Alert (typeof next); Undefined
~function Next () {
Alert (typeof next); function
}()
}()
The purpose of adding a ~ in front of the function is to make the function declaration into a function expression, and you can actually add other operators, such as +,-,!, and so on, the function declaration is turned into a function expression.
The first alert out of print results is undefined, which shows that next does not have a function promotion at all.

Next, verify that:

A ();//123
var a = function () {
Console.log (321);
}
A ();//321
function A () {
Console.log (123);
}
As can be seen from the results, the first print out of the inverse is placed in the back of a (), the equivalent of the above code is indicated as follows:

var a = function A () {
Console.log (123);
}
A ();
A = function () {
Console.log (321);
}
A ();

Can prove that the expression does not ascend, and the function is promoted.


4. What is the higher priority if the variable promotion and function promotion occur simultaneously? Let's do an experiment:
Console.log (GGG); function GGG () {}
var ggg = 222;
function GGG () {};
Console.log (GGG); 222
Console.log (GGG); function GGG () {}
function GGG () {};
var GGG = 111;
Console.log (GGG); 111


function GGG () {};
var ggg;
Console.log (GGG); function GGG () {}

By the above code analysis: You can summarize the following rules: (The Great God blog summary, after seeing that really clear a lot)

1) variable declaration promotion : variable declarations are completed when entering the execution context.

Whenever a variable is declared in the code, regardless of where it is declared, the JS engine places its declaration at the top of the range scope;

2) function declaration Elevation : The function declaration is read before executing the code, meaning that the function statement can be placed after the statement that called it.

As long as the function is declared in the code, no matter where it is declared, the JS engine will place its declaration at the top of the scope scopes;

3) variable or function declaration : The function declaration overrides the variable declaration, but does not overwrite the variable assignment.

The same name identifies a, a variable declares var A, and function A () {} is declared, regardless of the order in which they are declared, the function declaration overrides the variable declaration, that is, the value of a is declared function A () {}. Note: If a is initialized at the same time as a variable declaration, or after a is assigned to a, the value of A's value variable at this time. Eg:var A; var c = 1; A = 1; function A () {return true;} console.log (a); 1





**********************************************
There will be some headaches to think about for a long time/(ㄒoㄒ)/~~

var zzz = 222;
function zzz () {
Console.log (111);
}
Console.log (ZZZ); 111
**********************************************
var zzz;
function zzz () {
Console.log (111);
}
Console.log (ZZZ); function zzz () {console.log (111);}
Console.log (ZZZ ()); 111, undefined
************************************************
B = 100;
function B () {
B = 2;
Console.log (B);
}
B (); b is not a function has been promoted, so with the function body in B = 100; so b is a variable, not a function.
************************************************
B = 100;
var B = function () {
B = 2;
Console.log (B);
}

****************************************************
The little exercises you see on the Great God's blog:
var a = 1;
Function B () {
Console.log (a);
A = 10;
function A () {}
}
b ();//?
Console.log (a);//?
My answer:   undefined  , 1
Result after run: function A () {}, 1


A = 10;
(function A () {
A = 1;

})();

My answer:
Post-run Result: function A () {a = 1; Console.log (a);} Here self-executing functions, first he is an expression, he does not ascend, while declaring functions and variables, function-based, return function
******************************************************
function A (i) {
Console.log (i);//?
var i = 1;
Console.log (i);//?
};
A (10);
My answer: 10, 1
Post-run Results: 10, 1 Oh, I'm finally on one.













JS functions and the promotion of variables

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.