Call and apply

Source: Internet
Author: User

Which four cases cannot use this
1, between rows, 2, set of one layer, 3, timer; 4, binding

The value used to set this

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>Title</title></Head><Body></Body></HTML><Scripttype= "Text/javascript">    functionShow (A, b) {Console.log ("this is"+ This+", A is"+a+", B is"+b); } Show (1,2);//window    //the parameters in call can be multiple, the first is this, and the following parameters are listed down    //Show.call (this, parameter)Show.call ( A,1,2);// A    //The role of call is similar to apply, but it only has two parameters, the first is this, the second is an array    //show.apply (this,[parameter]);show.apply ( A,[1,2]);//    //The difference between call and apply is only in the method of parameter passing. </Script>

Where to use:

functionmyaddevent (obj, SEv, fn) {//To sum up is://attachevent--compatible: IE7, IE8, not compatible with Firefox, Chrome, IE9, IE10, IE11, Safari, opera//addeventlistener--compatible: Firefox, Chrome, IE, Safari, Opera; incompatible IE7, IE8    if(obj.attachevent) {obj.attachevent (' On ' +sev,function() {fn.call (obj);//If you do not need to call,this point is window, use call to ensure that this point to the current obj        }); }    Else{obj.addeventlistener (SEv, FN,false);//to bind an element to an event, a function    }}

Call and apply

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.