JavaScript error-prone knowledge point collation

Source: Internet
Author: User
Tags closure variable scope

1. Variable Scope
var a = 1; function Test () {    var a = 2;     // 2 }test ();

The above function scope declares and assigns a, and above the console, so follows the nearest principle output a equals 2.

var a = 1; function test2 () {    //  undefined    var a = 2;} Test2 ();

Although a is declared and assigned in the function scope above, but under the console, a variable is promoted, the output is declared but not yet assigned, so the output is "undefined".

var a = 1; function test3 () {    //  1    = 2;} Test3 ();

A in the upper function scope is re-assigned, is not re-declared, and is under the console, so output a in global scope.

The concept of variable lifting is only applicable to statements that declare variables, and the statement of variable assignment cannot be advanced (if the execution statement can be arbitrarily swapped order, it is destructive imaginable)

We are used to seeing ' var a = 3 ' as a statement. In fact, here are two statements of shorthand, ' var a ' and ' A = 3 ', and actually these two statements are two different types of statements, is done by two different components. The previous sentence is executed during the compile phase, and the latter sentence is executed during the run phase

So, no matter where ' var a ' is written, it will be processed before the code itself is executed. This process is much like a moving process of code, so it is called "variable promotion" by everyone.

2. Type comparison
var arr = [],    = [1//  false

Above two different array comparisons, console is false.

var arr = [],    =//  false

Above two identical array comparisons, console is false.

Reason:

Because two separate arrays are never equal

var arr = [],    = {};console.log (typeoftypeof//  True 

The above uses typeof to compare arrays and objects, because typeof gets null, array, object type is object, so console is true

var arr =instanceof//  trueinstanceof// true

Above uses instanceof to determine whether a variable belongs to an instance of an object, because the array is also one of the objects in JavaScript, so two console is true.

3.this Pointing

My blog

Http://www.cnblogs.com/QianBoy/p/7599202.html

4. Closure issues
var // if there are 5 div on the page  for (var i = 0; i < elem.length; i++) {    function  () {        //  always 5
        };}

Above is a very common closure problem, click on any div pop-up value always 5

var // if there are 5 div on the page  for (var i = 0; i < elem.length; i++) {    (function  (w) {        function c11> () {            ///  0,1,2,3,4        };    }) (i);}
Conclusion

Learning JavaScript is a lengthy process and cannot be accomplished overnight. I hope this article will introduce some of the content can help students learn JavaScript more in-depth understanding and mastering JavaScript grammar, less detours.

JavaScript error-prone knowledge point collation

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.