JavaScript promotion (JavaScript you don't know) __java

Source: Internet
Author: User
Recently, in reading "You do not know the JavaScript (roll up)" This book, the book elaborated in detail JavaScript many important but often overlooked points, here strongly recommended ... In the book, the 4th Chapter describes "Ascension", starting with the example of the process of variable and function ascension, correcting the previous mistaken understanding (believing that many people understand it is a mistake).
We used to think of var a = 2 as a declaration, and the JavaScript engine actually doesn't. The following examples let you thoroughly understand the variable elevation in JavaScript.
Example 1:
A = 2;
var A;
Console.log (a);	2
Example 2:
Console.log (b); Undefined
var b = 2;
Example 3:
var C;
Console.log (c);	Undefined
c = 2;
Example 4:
Foo ();
function foo () {
	console.log (d);	Undefined
	var d = 2;
}
Example 5:
Bar ();	TypeError
var bar = function too () {
	//...
}
Example 6:
Aoo ();	TypeError
Boo ();	Referenceerror
var aoo = function Boo () {
	//...
}
Example 7:
function foo () {
	console.log (1);
}
Foo ();	1
foo = function () {
	console.log (2);
};
Example 8:
Foo ();	3
function foo () {
	console.log (1);
}
var foo = function () {
	console.log (2);	
};
function foo () {
	console.log (3);
}
Example 9:
Foo ();	b
var a = true;
if (a) {
	function foo () {Console.log ("a");}
} else{
	function foo () {Console.log ("B");}
}

Note: This behavior is not reliable and may change in future versions of JavaScript, so you should avoid declaring functions within the block as much as possible. Summary:
1. var a = 2; where Var A is in the compile phase, a=2 in the implementation phase;
2. Regardless of where the declaration (variables and functions) in the scope appears, it is processed first before the code itself executes;
3. The declaration itself is promoted, and assignment operations, including the assignment of function expressions, are not elevated;
4. The function will be promoted first, then the variable, and the repeated var (variable) declaration will be ignored;
5. The following function declaration can overwrite the previous.
PS:
1. The RHS query throws Referenceerror when the desired traversal is not found in all nested scopes.

2. RHS queries to a variable, but you try to manipulate it unreasonably (referencing a property in a null or undefined type) that throws a typeerror.


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.