10 code to get through JS learning of the two meridians

Source: Internet
Author: User

Objective

In order to prepare for node. js, JS's basic skills are still very important. So when the 1024 programmer's Day, so I looked for some questions, organized a bit of knowledge.

Simple callback

Code

function foo () {

Console.log (THIS.A);

}

function Dofoo (FN) {

FN ();

}

function DoFoo2 (o) {

O.foo ();

}

var obj = {

A:2,

Foo:foo

};

var a = "I ' m an A";

Dofoo (Obj.foo);

DoFoo2 (obj);

Analysis

In JavaScript, this points to the current object when the function executes, not to the declaration environment.

The execution environment is the Dofoo function when executing the dofoo, and the execution environment is global.

When executing DoFoo2, the function is called inside the object, and the this pointer points to the object.

Results

I ' m an A

2

Change function scope with apply

function foo (somthing) {

Console.log (THIS.A, somthing);

}

function bind (FN, obj) {

return function () {

return fn.apply (obj, arguments);

}

}

var obj = {

A:2

}

var bar = bind (foo, obj);

var B = Bar (3);

Console.log (b);

Analysis

Apply, call, bind all have a role is to change the scope, here with Apply the Foo function to the scope of the Obj object, and passed in parameters.

After a simple analysis of the internal nesting of the bind function, the execution of the BIND function returns an anonymous function, so executing bar (3) is actually an anonymous function inside the bind that was executed, returning the result of the previous Foo function's execution.

Default returns undefined if the function does not return a value.

Results

2 3

Undefined

New keyword

function foo (A, b) {

This.val = a+b;

}

var bar = Foo.bind (null, ' P1 ');

var baz = new Bar (' P2 ');

Console.log (Baz.val);

Analysis

The first parameter of the BIND function is null for the scope, and subsequent indeterminate arguments are bound to the parameters of the function itself in order, and after the binding the execution function can only start passing values that are never bound.

Results

P1p2

Self-executing functions

function foo () {

Console.log (THIS.A);

}

var a = 2;

var o = {A:3,foo:foo};

var p = {A:4};

(P.foo=o.foo) ();

Analysis

You can often see this code.

(function () {

//...

})()

This code usually creates an immediate function and avoids polluting global variables.

Few people pay attention to what results will be returned after the assignment statement is executed, in fact, to return the current value. That is, when the assignment is completed in parentheses, the Foo function in the O object is returned. There is an A object in the execution environment of the function, well, that's it.

Answer

2

Variable Properties

var a = [];

A[0] = 1;

a[' foobar '] = 2;

Console.log (a.length);

Console.log (A.foobar);

Analysis

When a variable is declared, expanding its properties does not alter the Tableland data type.

Results

1

2

Accuracy issues

var a = ' foo ';

A[1] = ' O ';

Console.log (0.1+0.2==0.3| | a);

Analysis

When working with decimals, be careful, the decimal calculation of JS is not accurate, so the above judgment is false.

A string variable is a constant.

Results

Foo

naming promotion

Foo ();

var foo = 0;

function foo () {

Console.log (1);

}

foo = function () {

Console.log (2);

};

Analysis

Declared variables and named functions are promoted to the very front of the code, except that the assignment statements of the declared variables are in the same position in the code. So the above code should be understood as:

var foo;

function foo () {

Console.log (1);

}

Foo ();

foo = 0;

foo = function () {

Console.log (2);

};

Results

1

Thinking

Foo ();

var foo = 0;

function foo () {

Console.log (1);

}

Foo ();

foo = function () {

Console.log (2);

};

Foo ();

The result of the above code:

1

Error

Scope

Foo ();

var a = true;

if (a) {

function foo () {

Console.log (' a ');

}

} else {

function foo () {

Console.log (' B ');

}

}

Analysis

JavaScript is not scoped to code snippets, but rather to functions.

And then according to the name Ascension of the Tableland, so this code should be this:

function foo () {

Console.log (' a ');

}

function foo () {

Console.log (' B ');

}

Foo ();

var a = true;

if (a) {

} else {

}

Results

B

Closure traps

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

SetTimeout (function () {

Console.log (i);

}, i*1000);

}

Analysis

An important function of closures is that the variables of the outer function are not persisted when the inner layer function refers to the variable defined by the outer function.

Here's a hidden trap, which is that after the for loop I'm still self-increasing by 1.

Results

6

6

6

6

6

Pseudo closed Package

function foo () {

Console.log (a);

}

function Bar () {

var a = 3;

Foo ();

}

var a = 2;

Bar ();

Analysis

A closure is a nested definition of a function, not a nested invocation of a function.

Results

2

Thinking

How to Output 3?

function Bar () {

function foo () {

Console.log (a);

}

var a = 3;

Foo ();

}

var a = 2;

Bar ();

10 code to get through JS learning of the two meridians

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.