Look at Uncle Tom's "Do you really understand JavaScript?" "Some feeling of

Source: Internet
Author: User

Look at Uncle Tom's "Do you really understand JavaScript?" ", there are 5 of questions, I have done it all, and then run it in Chrome's console, although only the wrong one, but still carefully read the next answer, in this summary, to see if we understand the underlying principles of these javascript.

Topic One (All Global variables are window properties, variable declarations are ahead of time, variable assignments are not advanced)
if inch window)) {    var a = 1;} alert (a);

Because the feature in JavaScript is ahead of the variable declaration, in fact the above code is equivalent to the following:

        var A;         if inch window)) {            = 1;        }        alert (a);

Because all global variables are properties of the window, they do not enter the loop body and do not execute a=1 which is why the answer is undefined

Topic two (function declaration in advance, function expression equivalent to variable assignment so no advance, the function declaration overrides the variable declaration, but does not overwrite the variable assignment)
var a = 1,    function  A (x) {        && A (--x);    }; alert (a);

The answer to this question is 1. In fact the code above is equivalent to the code below

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

In the second line of the original topic, B and a point to a place that is the entrance to the function, but the only difference between A and B is that the function definition ends, and a is not referenced, that is, the scope of a is only within the function body, while the scope of B is in the whole global scope. Look at the picture and talk

Another important concept in this is that the function declaration overrides the variable declaration, but does not overwrite the variable assignment. Look at the following example:

function value () {    return 1;} var Value;alert (typeof value);    // "function"

As soon as the variable declaration is defined below, but the variable value is still function, that is, the function declaration takes precedence over the variable declaration priority, but if the variable value is assigned, the result is completely different:

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

Topic Three (function declarations with the same name, function variables are not redefined)
function A (x) {    return x * 2;} var A;alert (a);

I believe you understand the title two of the comments, this question will certainly, put the answer.

Topic Four (callee and caller and some relationships of function parameters)
function b (x, Y, a) {    arguments[2] = ten;    alert (a);} B (1, 2, 3); //Results are Ten

In fact, arguments is similar to an array, it can be accessed through the square bracket syntax of each of its elements, in addition arguments and named parameters can be used together, they are shared, but this share is not really shared a memory address, but instead of 2 different memory addresses, Using the JavaScript engine to ensure that 2 values are always the same, so modify the value of the arguments is also reflected in the named parameters, of course, there is a premise, that is, the index value is less than the number of parameters you passed in, that is, if you only pass in 2 parameters, and continue to use arguments[2] assignment, it will be inconsistent, see the following code:

function b (x, Y, a) {    arguments[2] = ten;    alert (a);} B (1, 2); // At this time because the third parameter A is not passed, so after assigning 10, alert (a) is still undefined, not 10, but the following code pops up the result is still 10, because it is not related to a. 

function b (x, Y, a) {    arguments[2] = ten;    Alert (arguments[2]);} B (1, 2); // The result is still ten

However, it is not allowed to modify the value of arguments in strict mode

Topic Five (the related concepts of this)
function A () {    alert (this);} A.call (null);

This is the object that is currently called, that is, if the method is a property of an object, the this in the method points to the object, which is the current object at run time. If a method is a global function, the this in the method points to the window

The call method is mainly used to change the scope chain, and the calling method as a function execution means that the method can be called by another object as the caller, the first parameter of the call method is the object caller, The other arguments that follow are passed to the calling method parameter (if declared), according to the ECMASCRIPT262 specification: If the object caller passed in by the first argument 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.

So the answer to this question is [object Window]

Look at Uncle Tom's "Do you really understand JavaScript?" "Some feeling of

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.