Do you think you understand JavaScript?

Source: Internet
Author: User

An article on Dmitry Baranovskiy's blog (http://dmitry.baranovskiy.com/post/91403200), which has five small pieces of code to test for understanding the core of JavaScript, closures and scopes , the article also received attention and discussion in the CSDN forum, brainstorming, the following combination of their own understanding, done the following summary.

1

if (! ("a" in window)) {    var1;} Console.log (a);

The program will first parse all declared functions, followed by Var declared variables, because JavaScript does not have a block concept, so if () {...} , var declares a = 1 and a is still a global variable.

Execution is equivalent to:

var // Global if (! ("a" in window)) {    1;} Console.log (a);

(1) At the beginning, the variable A is declared, but it is not assigned, so a = undefined, and undefined exists in the window, so (' a ' in window) returns True, which is reversed to false, so that the "a=1" statement inside the curly braces is not executed.

(2) Console.log (a); //undefined

2

var 1 ,     = function A (x) {        && A (--x);    }; Console.log (a);

A var can be used to declare multiple variables, separated by multiple commas, and executed equivalent to:

var 1 ; var b = function A (x) {    && A (--x);} Console.log (a);

(1) The function expression is similar to the local variable, is not accessed by the global scope, so the function A is a local variable, external unreachable, so global a or 1;

(2) Console.log (a); //1

3

function A (x) {    return2;} var A;console.log (a);

JavaScript is always the first to parse the declaration function, then parse the variables, and execute the following sequence:
(1) analytic function A;
(2) declaring variable var A; Because a is not assigned at this time, so it is undefined, or point to the original value, that is, function A;
(3) Console.log (a); //function a

4

function B (x, Y, a) {    arguments[2;    Console.log (a); }b (123);

Inside the function, you can refer to an object of an array of classes arguments, which is not a real array, represents a collection of parameters that the function actually accepts, and can be accessed by subscript to the corresponding parameter.
If you modify the properties of this object, such as Arguments[index], the value of the variable that is passed in index (subscript starting at 0) is also modified.
Execution order:

(1) Declaring a function B;
(2) Execute function B (arguments[2), because here [] and variable a refer to the same value, so when arguments[2] changes, a also changes.
(3) Console.log (a) //;

5

function A () {  Console.log (this);} A.call (null);

Call invokes one method of an object, replacing the current object with another object.

Formats such as Call (Thisobj, ARG1,ARG2...ARGN);

The call () method is called outside the function body, and if NULL is passed in, the default is to window, and if not, the this in the function points to the window.
Console.log (This) //window;

function A () {    Console.log (this = =  window);} Console.log (this//  true//  truea.call (null//  truea.call (this//  true//  true//  True


6

function fo () {    console.log (a);} function foo () {    var2;    Fo ();} Foo ();

The Foo function is executed first, although FO is called in Foo, but the FO function is declared under the global scope, the A referenced in fo is a window pointing to the global, and a in the global scope is not declared, although Var a=2 is declared under Foo, but as a local variable, cannot be called by a scope outside the function.
Console.log (a) // ais not defined;

If you write the above code:

function foo () {    var2;    function fo () {        console.log (a);    }    Fo ();} Foo ();

Because of this time, the function fo is declared in the Foo function body, belongs to the internal function of Foo, the scope chain is accessed from outside, a in the FO search, will go to the upper function foo, find var a = 2 and return the result.

Console.log (a) // 2;

Do you think you understand JavaScript?

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.