JS in three default methods Call,applay,bind

Source: Internet
Author: User

These three are all functions of the method (Function.prototype), these three methods can change the function inside this point,

Pager

//The call method receives three parameters, the first is this point, the second, and three are arguments passed to the function, which can be numbers, strings, arrays, and other types of data types.functionfn (n1,n2) {Console.log ( This); Console.log (N1,N2)}fn.call ();//=>this:window;Let obj ={fn:fn};fn.call (obj);//=>this:obj;n1,n2:undefined(Fn.call);//=>this:1;n1=2,n2=undefined;Fn.call (obj,1,2);//=>this:obj;n1=1,n2=2;//several special properties of the call method//non-strict modeFn.call (undefined);//This=>windowFn.call (NULL);//This=>window//under strict mode"Use Strict"fn.call (undefined);//this=>undefinedFn.call (NULL);//This=>null

Apply

// the use of the Apply method and the call method are basically the same, the only difference is that the application method of the parameter request is an array type, the array can be arbitrary form of data function fn (n1,n2) {    Console.log (this);    Console.log (n1,n2)    = {FN:FN};
Fn.applay (abj,[1,2]);
Fn.applay (abj,1,2);//Error

Fn.applay (ABJ,[one, ' Apply ', {a:123}]); // Note that the second argument must be an array, or it will be an error

Bind

//bind and call method calls are similar, but the principle is completely differentFn.call (obj,10,20);//=>FN is executed first, the this in FN points to obj, and the parameter 10,20 is passed to FNfn.bind (obj,10,20)//Bind is the first in the FN to point to obj, and the parameter 10,20 is pre-passed to FN, but the FN is not executed at this time, only when FN executes this point and pass parameters are only usefulFn.bind (obj,10,20);//= = will not have any output Fn.bind (obj,10,20) ();//+ = will not have output until called//= demand: When you click on the box, you need to execute FN and have this point in FN pointing to objOBOX.ONCLICK=FN;//+ = Executes fn when clicked, but at this point the this in FN is OboxObox.onclick=fn.call (OPP);//= = When binding an event, the FN executes immediately (call itself executes the function immediately) and binds the return value of the FN execution to the eventObox.onclick=fn.bind (OPP);//=>fn.bind (OPP): FN takes the Bind method on the Function.prototype and executes this/**function(){ *Fn.call (OPP);* } */Obox.onclick=function(){   //=>this:oboxFn.call (OPP);}

JS in three default methods Call,applay,bind

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.