Pretty test the basis of the JS pen question (have pit careful!) )

Source: Internet
Author: User

1. Review this

1 varLength = 102 functionfn () {3Alert This. Length)4 }5 varobj = {6Length:5,7Methodfunction(FN) {8FN ()// ?9Arguments[0] ()// ?Ten     } One } AObj.method (FN)

The pit here is mainly arguments, we know that the object belongs to the exception of the point operator can also be used in brackets, where the scope of FN is arguments, that is, FN within the this===arguments, called only one parameter FN, so length is 1.

2. When a function expression is named (the function declaration is assigned to another variable) or the function declaration is executed immediately, the name is accessible only within the function

 1  ~< Span style= "color: #0000ff;" >function   () { 2  alert (typeof  Next) // ?  3  ~function   Next () { 4  alert (typeof  Next) // ?  5   6  }()

The outer layer anonymous function executes, prints next, and then the inner named function executes itself. You will find that the named next is accessible only within its own function, that is, the output is function. Outside is invisible, typeof for undefined. ( Note: This problem IE6/7/8 output as function function, standard browser is undefined function)

The same happens when assigning a named function to a variable, as follows:

1 var function A () {2     alert (typeof  a)3}4//  ? 5 alert (typeof//  ?

This rule is clearly stated in the standard (ES3/ES5), but IE6, 7, and 8 are not strictly complied with. See also W3help's analysis or Lisongfeng teacher's translation "The Quest for the expression of a named function"

3. Add attributes to basic type data without error, but undefined when value is taken

1 A = 32 a.prop = 43//  ?

The variable A is the number 3, add the Prop property to it, and the value is 4 (oddly enough, this is allowed in JS and there is no syntax error). Then alert out the results of A+a.prop. The result is Nan. The A.prop is Nan for undefined,3+undefined.

Extrapolate, add a property to the string:

1 str = ' A '2 str.prop = ' B '3//  ?

The result?

4. Implicit Global variables

1 var a = 12function  func () {3     a = b = 24}5 func () 6 alert (a) 7 //  ?

The variable that is not declared by Var in JS is the global variable by default, and the connection here makes the situation more subtle. The b here is global, so it can be accessed outside of Func.

5. Variable declaration earlier than code run (scoping and hoisting)

1 var uname = ' Jack '2function Change () {3     //  ? 4     var uname = ' Lily '5    alert (uname)6}7 Change ( )

It is easy to be confused here is the first alert, if you think that the function change has been declared assignment, this should be jack, the actual function is also var once (although Var is behind), the pre-parsing will still be undefined. This is also why the book suggests that variable declarations are placed at the front of the code block.

6. Function declaration is earlier than variable declaration

1 function Change () {2     alert (typeof//  ? ) 3     function fn () {4         alert (' Hello ')5    }6     var fn 7 }8 Change ()

The change first alert out FN, post function declaration, and then variable declaration. If FN does not have a function declaration but only a variable declaration, the result is as undefined as 5. But here it is function. In the same scope, the function declaration is not related to the code block, and the function can be used normally. The variable declaration needs to be pre-set before the first use is undefined.

Pretty test the basis of the JS pen question (have pit careful!) )

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.