Examples of JavaScript closures and scopes, and details of javascript

Source: Internet
Author: User

Examples of JavaScript closures and scopes, and details of javascript

This article analyzes JavaScript closures and scopes. We will share this with you for your reference. The details are as follows:

1.

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

[Analysis] Code meaning: if the window does not contain attribute a, it declares a variable a and assigns a value of 1.

① The JS engine will first scan all the variable declarations

② All global variables are the properties of window.

③ When variables are declared and assigned values, the Js engine automatically divides them into two parts: Variable declaration in advance, no variable assignment (do not advance the assignment because it may affect the code execution to produce unexpected results)

Therefore, the code execution sequence is equivalent

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

Resolution: declare variable a to determine whether a exists. If it does not exist, assign a value to 1. Here, a will always exist in the window, and the value assignment statement will never be executed. Therefore, the result is undefined.

2.

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

[Analysis]

① The function declaration will also advance and overwrite the variable declaration, but will not overwrite the variable assignment, as shown in 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 attributes, and the statement var a = 1 is equivalent to window. a = 1;

Therefore, the code execution sequence is equivalent

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 variable definition in the function declaration is defined only when the function is executed, and the variable is not declared in the pre-compilation phase.

3.

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

[Analysis]

① Difference between function declaration and function expression

Function declaration:

Function functionName (arg1, arg2) {// function body}

Function expression (equivalent to variable assignment ):

Var functionName = function (arg1, arg2) {// function body };

Therefore, the code execution sequence is equivalent

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 stage of execution Context

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

The sequence of this phase: the form parameter of the function-> function declaration-> variable Declaration

② Code execution stage

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

③ Define the variable B and assign a value to the function named a (this a can only be used in the function body)

④ If x is any true value (Here it should be not 0), execute the following statement

⑤ Change to a more understandable code

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

Result 1

5.

function b(x, y, a) {  arguments[2] = 10;  alert(a);}b(1, 2, 3);

[Analysis] No difficulty. Output 10. If changed

function b(x, y, a) {  arguments[2] = 10;  alert(a);}b(1, 2);

The output is undefined because the value of a is not passed.

6.

function a() {  alert(this);}a.call(null);

[Analysis]

①. Call (B) indicates that the method of object a is applied to object B (that is, object B inherits object a). According to regulations, the first parameter is null or undefined, the call method regards the Global Object (window) as the value of this.

② Difference between call () and apply (): The function is the same. The second parameter is in different forms. Multiple parameters are transmitted in call. in any form, the second parameter must be in array format, for example

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

Output result: [object Window]

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.