JavaScript details Success or failure

Source: Internet
Author: User

1. var

var is well known for defining variables such as Undefined,number,string,bool,array,function,object,null.

But sometimes for the sake of convenience, there are some memory leaks:

function Fun () {

var a=b=1;//for immediate assignment

}
Fun ();

Console.log (a);//undefined;

Console.log (b);//1 at this point, B becomes a global variable.

function Fun () {

A=1;

var a=2;

Console.log (a);//2;

}
Fun ();

Console.log (a);//undefined; Compile-time variables go to the first line of the context by default, so a is not a global variable

2. For loop

var arr=[1,2,3];

for (Var i=0;i<arr.length;i++) {

Arr.push (i);//A Dead loop occurs, an array push value changes its length, and for is always calculated Arr.length

1, waste consumption performance

2, loop to modify the array is not aware of the situation will be unexpected error

}

Console.log (i);//4 variable I may not be noticed, and the scope of the context persists.

After modification

var arr=[1,2,3];

var I,max;

for (i=0,max=arr.length;i<max;i++) {

Arr.push (i);

}

Console.log (i);//4

Console.log (arr);//[1, 2, 3, 0, 1, 2]

3. New

var person=function (name,age) {

This.name=name;

This.age=age;

}

var per=new person (' Zhang San ', 17);


The actions performed by the above new:

First, create a new object;


The scope of the constructor is assigned to the new object (so this points to the new object);


Third, execute the code in the constructor (add attributes for this new object);


Return the new object to the value of per (this points to per);


4. function

function is either a constructor or an object.

var fun=function () {

Console.log (THIS.A);

this.c= "345";

};//function

Fun.a= "123", fun.b= "234";//Object

var tempfun=new fun ();//constructor Output 123

Console.log (TEMPFUN.A);//undefined


5, this

Execution context (EC): Control enters an execution context whenever control is transferred to a piece of executable code. The return of each function exits the current execution context.

This: an attribute in the execution context (EC) that is determined when the context is entered.

Closure Classic Example

var temp=1;

function Fun () {
Console.log (this);
var temp=0;
return function () {

Console.log (this);

This.temp+=1;
Console.log (this.temp);
}
}

var fun2=new fun ();//new creates a new object and executes the function run-time this points to the creation object (unnamed, debugger is represented with fun) and assigns the value to Fun2

Fun2 ();//(Because FUN2 is a global object, or This=window) output 2

6, () brackets

1, (a+b) expression

2, A () Enter the execution context (run function)

How do you tell?

My understanding is when "(" Front is + 、-、 *,/,%, =,! is treated as an expression when the operator is used as an execution function.

7, [] Index

var a=1;

a[0]=2;

Console.log (A[0]);//undefined

JavaScript all objects will provide get and set functions ("Reference type" Object function array string etc can get to value, but "value type" number not)

8. {}

1. Create a new object Var obj={};

2, create a local cloth context (only function can)

for (int i=0;i<2;i++) {

var a=i;

}

Console.log (a);//2 for, switch, if, etc. loops cannot be created

9. Execution context, variable object, scope chain

1. Execution context (EC) It says that only function can create a local context, which becomes the execution context when the current context is loaded into memory.

2. Variable object (VO) declaration in the context of the special object storage associated with the execution context

2.1. Variables (variables declared within a function)

2.2. Declaration of functions

2.3, the function of the formal parameters

3. Scope scope is a list of all variable objects (including parent variable objects) in context

JavaScript details Success or failure

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.