Chapter V web front-end development engineer --JavaScript Advanced Programming 5-8 JavaScript call and apply

                                                                             JavaScript call和apply

 

This lesson is to talk:

  1. JavaScript this point to the problem of
  2. JavaScript call和apply
  3. javascript New use

                                                                          Speaker teachers: Head teacher

A. JavaScript this points to a problem

Within the function:

function f(){

this.name=ass;

}

Within an object:

There he = {

print:function(){

console.log(this.name);

}

}

DOM operations:

Var ad= document.getElementById(“ad”);

ad.onclick=function(){

console.log(this);

}

T His-point change

This.name=“global”;

PS: the principle of this 

1. Run-time decision  , this points to an object.

2. Who function in this point in the end, determined by the object when this function is called, rather than the object defined by the function where the decision.   

Two. JavaScript call and apply

call method:

Syntax: call ([thisObj [, arg1 [, arg2 [, [, .argN]]]]])

It is defined: a call to an object method, replace the current object to another object.

Description:

The method may be used instead call another object calls a method. The method may call a function of the object context specified by the new object from the initial thisObj context change.

If no thisObj parameters, then the Global object is used as thisObj.

apply method:

语法:apply([thisObj[,argArray]])

Definition: Application of a method of an object, replacing the current object with another object.

Difference: The first parameter is the same meaning. The second parameter: apply a parameter is passed the array, i.e. the array into a plurality of combinations of parameters passed, and the call is an incoming call as a parameter (second parameter starts).

The func.call (func1, var1, var2, var3) corresponding to apply writing func.apply (func1, [var1, var2, var3]), while the benefits of use to apply the direct current as a function of the object arguments apply to the second two parameter.

function Obj(){

this.value="对象!";

}

var value = "global variable";

function Fun1(){

alert(this.value);

}

//window.Fun1(); //window

//Fun1.call(window); //window

//Fun1.call(document.getElementById('ad')); //ad  Head

//Fun1.call(new Obj()); //obj

 

var func = new function(){this.a="func"}

var myfunc = function(x){

var a = "myfunc";

alert(this.a);

alert(x);

}

myfunc.call (func, "var"); // Object

// func was

var func = new function(){this.a="func"}

var myfunc = function(x){

var a = "myfunc";

alert(this.a);

alert(x);

}

myfunc.apply(func,['var']);

Three. Javascript New use

new operator is used to instantiate a class to allocate a memory object instance.

One example of an object may be generated by the original object new, and this instance object inherits properties and methods of the original objects. Therefore, the meaning of existence is that it implements the new javascript in succession, not just instantiate an object!

 

Guess you like

Origin blog.csdn.net/wgf5845201314/article/details/92388881