These five questions, you know JavaScript, you're getting started.

Source: Internet
Author: User

1.

1 if inch window)) {2     var a = 1; 3 }4 alert (a);

Read the Code: If window does not contain property A, declare a variable A, then assign a value of 1, and the value of the variable is finally popped.

Understand the code: JavaScript does not have block-level scope, so var a=1; the definition of a is global, and because global variables are properties of the window (note that variables declared without Var are also global and window properties but cannot be deleted) , so the execution process is not in the if internal, and because the variable what the promotion, in fact, when executing if (Judgment statement), a actually already exists in the execution context of the variable object, the above code is equivalent to:

1 var A; 2 if inch window)) {3     a = 1; 4 }5 alert (a);

Again, the code: first declare a, then determine whether a is in existence (is not a window property), if it does not exist on the assignment is 1, it is obvious that a always exists in the window, the assignment statement will never execute, so the result is undefined.

2.

1 var a = 1,2     function  A (x) {3         x && A (---x); 4     }; 5 alert (a);

Read the code: first define the variable A and assign a value of 1, and then define a function expression to assign to B; But this function expression is a well-known function expression, where different browsers will have different parsing: in IE, the A is considered a function declaration, so it is overridden by the variable initialization (next talk about the variable coverage problem) , which means that if a (--x) is called, it will go wrong (because A is a variable), while other browsers allow a (--x) to be called inside the function, because a is still a number outside the function, and within the function is its own function declaration. The above code can actually not look at the B function expression, so the above code executes the result of alert 1.

1 var a = 1,2     function(x) {3         x && B (---x); 4     }; 5 alert (a);

Variable overwrite problem: The function declaration overrides the variable declaration but does not overwrite the variable assignment (the function declaration takes precedence over the variable declaration, but if the variable value is assigned)

 1  function       2  return  1;  3   4  var   value;     5  alert (typeof  value); //  
1 function value () {2     return 1; 3 }4var value = 1; 5 alert (typeof value);    // "Number"

3.

1 function A (x) {2     return x * 2; 3 }4var  A; 5 alert (a);

is the second question. function declaration override Variable declaration

4.

1 function b (x, Y, a) {2     arguments[2] = ten; 3     alert (a);//10; 4 }5 B (1, 2, 3);

First understand the arguments object, he has a three attributes:

    1. Callee-a reference to the current function
    2. length-true number of arguments passed
    3. The value of the Properties-indexes (integer of String type) property is the parameter value of the function, arranged from left to right in the argument list. Properties-indexes the number of internal elements is equal to arguments.length. The value of properties-indexes is shared with the actual passed in parameters

When the parameter of the passed-in function is less than the index value of argument, such as when you pass in only 2 parameters and continue to use arguments[2] assignment, it will be inconsistent.

1 function b (x, Y, a) {2     arguments[2] = ten; 3     alert (a);//undefined;
4 alert (arguments[2]);//10; 5 }6 B (1, 2);

5.

1 function A () {2     alert (this); 3 }4 a.call (null);

This problem has several points: this, call, NULL;

this--who calls on who to point;

call--The first parameter changes this point;

null--refers to the window.

Note the null here: According to the ECMASCRIPT262 specification: If the first parameter passed in to the object caller is null or undefined, the call method takes the global object (that is, window) as the value of this. So, no matter when you pass in NULL, its this is the Global object window;

1 function A () {2     alert (this);//window; 3 }4 a.call (window);

These five questions, you know JavaScript, you're getting started.

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.