You don't know. JavaScript notes (1)

Source: Internet
Author: User

Conflict avoidance

function foo () {

function Bar (a) {

i = 3;

console.log (A + i);

}

for (var i=0; i < i++) {

bar (i * 2)

}

}

11 Infinite Dead Loop

The simplest way to differentiate function declarations from function expressions is to look at where the function keyword appears, if the function is the first word in the declaration, then it is a functional declaration, otherwise it is a function expression.

(function foo () {}) () function expression that executes immediately

Iife executing function expressions immediately

var a = 2;

(function Iife (global) {

var a = 3;

Console.log (a); 3

Console.log (GLOBAL.A)//2

}) (window)

Console.log (a)//2

Passing a reference to the Window object, of course, can be passed in from an external scope into anything you need,

And you can name the variable whatever you think is appropriate.

var a = 2;

(function Iife (def) {

def (window)

}) (Function def (global) {

var a = 3;

Console.log (a); //3

Console.log (Global. A) //2

});

The function expression def is defined in the second part of the fragment, and then as a parameter is passed into the first part of the Iife function definition. The last Def, which is passed in, is called, and the window is passed in as the value of the global parameter.

The catch clause of a try/catch creates a block-level scope in which the declared variable is valid only within the catch

Example: try{

Undefinde ();

}catch (Err) {

Console,log (ERR); can be used normally

}

Console.log (ERR)//Referenceerror:err not found

Let block scope

var foo = true;

if (foo) {

Let bar = foo *;

bar = something (bar);

Console.log (bar);

}

Console.log (bar); Referenceerror

A variable is promoted, preceded by a declaration and then assigned a value.

Foo ();

function foo () {

Console.log (a); undefinded

var a = 2;

}

Reason: equivalent to the following

function foo () {

var A;

Console.log (a); Undefined

A = 2;

}

Foo ();

Note: function declarations are promoted and function expressions are not declared. Both the function and the variable are promoted, but the function is promoted in advance and then the variable.

Foo (); 1

var foo;

function foo () {

Console.log (1);

}

foo = function () {

Console.log (2)

}

CONST constant

The following function declaration overrides the previous

Foo (); 3

function foo () {

Console.log (1);

}

var foo = function () {

Console.log (2);

}

function foo () {

Console.log (3);

}

var a = 2; The parsing principle is var A and a = 2 as two separate declarations, the first one is the compilation phase

Tasks, the second is the task of the execution phase.

Closed Package

function foo () {

var a = 2;

function Bar () {

Console.log (a);

}

return bar;

}

var baz = foo ();

Baz (); 2

VAR fn;

function foo () {

var a = 2;

function Baz () {

Console.log (a)

}

Bar (Baz);

}

Function Bar (FN) {

FN ();

}

for (var i = 1; i<=5; i++) {

(function (j) {

SetTimeout (function () {

Console.log (j)

},j*1000)

}) (i)

}

Using Iife inside an iterator will generate a new scope for each iteration.

Block-scoped and closed-packet teaming

for (var i = 1; I <= 5; i++) {

SetTimeout (function timer () {

Console.log (i)

},i*1000)

}

Module

function Coolmodule () {

var something = "cool";

var = [another];

function dosomething () {

Console.log (something);

}

function Doanoth () {

Console.log (Another.join ("!"));

}

return {

Dosomething:dosomething,

Doanother:doanother

};

}

var foo = coolmodule ();

Foo.dosomething (); Cool

Foo.doanother ()//1! 2! 3

Change to Singleton mode:

var foo = (function Coolmodule () {

var something = "cool";

var = [another];

function dosomething () {

Console.log (something)

}

function Doanother () {

Console.log (Another.join ("!"))

}

return {

Dosomething:dosomething,

Doanother:doanother

}

})();

Foo.dosomething (); Cool

Foo.doanother (); 1! 2! 3

You don't know. JavaScript notes (1)

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.