JS in the call apply Bind

Source: Internet
Author: User

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>Title</title></Head><Body></Body><Script>    vara={User:"chasing the dream son", fn:function() {Console.log ( This. User);    }    }; varb=A.fn; b (); //undefined//for what? Because JS in the direction of this, always look at the time of execution is who call him, but when the A.fn copy to B is not called, is in the window Global environment, the final point is window. //Call apply bind is to change the point of this problem in JS//1 Call    vara={User:"chasing the dream son", fn:function() {Console.log ( This. user);//chasing the dream son        }    }; varb=A.fn; B.call (a);//by using the call method, add B to the environment of a, in fact, let this continue to point to a environment. //The call method can add multiple parameters in addition to the first argument, as follows    vara={User:"chasing the dream son", fn:function(E,ee) {Console.log ( This. User); //chasing the dream sonConsole.log (E+ee); //3        }    }    varb=A.fn; B.call (A,1,2);//2 Apply    vara={User:"chasing the dream son", fn:function() {Console.log ( This. user);//chasing the dream son        }    }; varb=A.fn; B.apply (a);//with the same effect as call, add B to an environment, change this point to//Unlike call, apply can also specify parameters, but the parameters are in the form of an array//var a = {//User: "Chasing the Dream Son",//fn:function (e,ee) {//Console.log (This.user);//Chase Dream son//Console.log (e+ee);//11//        }//    }//var b = A.fn;//b.apply (a,[10,1]);//    *********************//Note If the first argument to call and apply is NULL, then this refers to the Window object//3 Bind    vara={User:"chasing the dream son", fn:function() {Console.log ( This. User);    }    }; varb=A.fn; B.bind (a);//bind can also change the point of this, we find that the code is not printed, yes,//This is the difference between bind and call, the Apply method, in fact the Bind method returns a modified function. //You also need to perform this function manually    varC=B.bind (a); C ()//OK, the same bind can have multiple parameters, and the parameters can be executed again when added, but note that the parameters are in the order of the formal parameter    vara={User:"chasing the dream son", fn:function(e,d,f) {Console.log ( This. User); //chasing the dream sonConsole.log (e,d,f);//Ten 1 2        }    }    varb=A.fn; varC=B.bind (A,Ten); C (1,2);//    ******************//Summary: Call and apply are all changes in the context of this and immediately execute this function,//The Bind method allows the corresponding function to think about when the call is made, and can add parameters to the execution.//This is the difference between them, according to their actual situation to choose to use. </Script></HTML>

JS in the call apply 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.