JS Learning Summary----The difference between call and apply and bind

Source: Internet
Author: User

Apply and call methods are identical, are used to change the method of the This keyword, and the method of execution: and in strict mode and non-strict mode for the first parameter is null/undefined the law of this situation is the same;

Bind: This method is incompatible with ie6-8 and call and apply are used to change the This keyword

' Use strict '//tell the current browser that the next JS code will be written in strict mode        varobj = {name: ' Zhang San '}        functionfn (num1,num2) {Console.log (Num1+num2); Console.log ( This)} fn (100,200);//This ->window num1=100 num2=200Fn.call (100,200);//this->100 num1 = num2=undefined NaNFn.call (obj,100,200)//This->obj Call When the parameter is passed to FN, it is passed a value, and apply is not a pass, but the parameter value to be passed in an array to operate. But it's also equivalent to assigning a parameter to FN.Fn.apply (obj,[100,200]);//        //pre-processing: The FN's this change in advance to the results we want, and the corresponding parameter values are also ready for use later, the direct execution can be. Bind here to achieve the effect of preprocessing        varTEMPFN = Fn.bind (obj,100,200);//just changed the this in FN to obj, and the FN passed two parameter values 100 and 200, but at this time does not have the FN function, execute bind will have a return value, this return value TEMPFN is we put FN's this change after the resultTEMPFN (); Fn.call ();//This->window in strict mode this->undefinedFn.call (NULL);//This->window in strict mode this->nullFn.call (undefined)//This->window in strict mode this->undefined

JS Learning Summary----The difference between call and apply and 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.