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