Call and Apply methods in JavaScript

Source: Internet
Author: User

1 /*2 in JS, call and apply are commonly used for binding scopes3 */4 //1 Simple Bindings5 functionsum (A, b) {6     returnA +b;7 }8 //bind the function of sum to test2 to perform9 functionTest2 (A, b) {Ten     returnSum.call ( This, A, b); One }             A //the difference between call and apply is the Apply receive array as a parameter - functionTest3 (A, b) { -     returnSum.apply ( This, [A, b]); the } -  -  -  + //2 Temporary binding caller decoupling -  varobj = { +Color: "Red", AName: "Z3" at  }; -   -  functionShowinfo () { -Alert This. color); -Alert This. Name); -  } in  //in JS, this specifies the caller. Which object calls the function this is which object -  //if we want to use Showinfo to operate with obj, we need to call this object with obj . to  //The function can be implemented by binding obj with call, which can reduce the coupling between the function and the object. +Showinfo.call (obj);//It 's like using obj to invoke this method, which improves the reusability of the Showinfo function . -  the  /* * The interior is actually: $ 1 bind obj to a function method = ShouwinfoPanax Notoginseng 2 Using Obj to execute method () - 3 Delete Method the such a process +  */

Call and Apply methods in JavaScript

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.