JavaScript closures and scope examples detailed _javascript tips

Source: Internet
Author: User

This article analyzes JavaScript closures and scopes in the example. Share to everyone for your reference, specific as follows:

1.

if (! () A "in Window") {
  var a = 1;
}
alert (a);

Analysis Code meaning: If window does not contain attribute A, declare a variable A and assign a value of 1

The ①JS engine scans all the variable declarations first.

② All global variables are window properties

③ variable declaration and assignment, the JS engine automatically divides it into two parts: the variable declaration is in advance, and the variable assignment is not (not to advance the assignment because he has the potential to influence the execution of the code to unexpected results)

So code execution order is equivalent to

var A;
if (! () A "in Window") {
  a = 1;
}
alert (a);

Parsing: Declaring a variable A to determine whether a exists, does not exist is assigned a value of 1, and here A is always in the window, the assignment statement never executes, so the result is undefined

2.

if (! () A "in Window") {
  function A () {WINDOW.A = 1}}
}
alert (a);

Analysis

The ① function declaration is also advanced and overrides the variable declaration, but does not overwrite the variable assignment, as the following example

function value () {return
  1;
}
var value;
Alert (typeof value);  "Function"

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

② All global variables are window properties, statement var a=1, equivalent to window.a=1;

So code execution order is equivalent to

function A () {WINDOW.A = 1;}
if (! () A "in Window") {
  function A () {var a = 1}}
}
alert (a);

Output Result: function A () {WINDOW.A = 1;}

Deformation:

if ("a" in window) {
  function A () {WINDOW.A = 1}}
}
alert (a);

Output result: A is not defined

The definition of a variable in a function declaration is not defined until the function is executed, and the variable is not declared at the precompiled stage

3.

if (! () A "in Window") {
  var a = function () {WINDOW.A = 1;}
}
alert (a);

Analysis

① function declaration and function Expression difference

function declaration:

function functionname (arg1, arg2) {
  //functional Body
}

function expression (equivalent to variable assignment):

var functionname = function (Arg1, arg2) {
  //functions body
};

So code execution order is equivalent to

var A;
if (! () A "in Window") {
  a = function () {WINDOW.A = 1;}
}
alert (a);

The result is the same as the first question, undefined

4.

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

Analysis

① enter the execution context phase

VO (Global) = {
  a:undefined,
  b:undefined
}

The order of this phase: the function's formal parameter-> function declaration-> Variable declaration

② Execute code phase

VO (Global) = {
  x:undefined,
  a:1
}

③ defines the variable b, assigning a function named a (this a can only be used in the function body)

④ if x is any true value (this should be a non-0 here) execute the following statement

⑤ to more understandable code.

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

Result is 1

5.

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

"Analysis" without difficulty, output 10, if changed to

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

The output undefined because no value of a is passed

6.

function A () {
  alert (this);
}
A.call (NULL);

Analysis

①a.call (b) The method that represents a object is applied to a B object (that is, the B object inherits a object), according to the rule, the first parameter is null or the Undefined,call method takes the global object (that is, window) as the value of this

The difference between ②call () and apply (): function, the second parameter form is different, call pass multiple parameters, any form, apply the second parameter must be an array form, such as

A.call (b,2,3); ==> a.apply (b,[2,3]);

Output result: [Object Window]

For more on JavaScript related content to view the site topics: "JavaScript object-oriented Tutorial", "javascript json operation tips Summary", "JavaScript switching effects and techniques summary", " JavaScript Search Algorithm Skills summary, javascript error and debugging skills Summary, JavaScript data structure and algorithm skills summary, JavaScript traversal algorithm and skills summary and JavaScript mathematical Operational Usage Summary

I hope this article will help you with JavaScript programming.

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.