A simple understanding of the call () apply () method

Source: Internet
Author: User

Really is very simple to understand, I know not much, in the Internet to find a lot of information, or only to understand a little fur, the following to organize out, to facilitate further in-depth to learn, but also to the current knowledge point of consolidation.

Sort out some of the classic answers on the Web:

1. One sentence distinguishes between call and applyObj1.call (OBJ2,ARG1,ARG2,ARG3) = = Obj1.apply (obj2,arguments) = = Obj2.foo (ARG1,ARG2,ARG3)ThisA.functionA.apply (thisb,[arg1,arg2,...]) = = ThisA.functionA.call (thisb,arg1,arg2,...) = = Thisb.functiona (arg1, Arg2,...)
    • Obj2 if empty this points to the Window object
    • The difference between call and apply is that the parameters of the call are fixed and entered in order. The parameter received by apply is an array.
    • When do I use call or apply? Calls are generally used when the parameter is fixed, and apply is required if the argument is indeterminate or if the parameter is an array.
    • The above words are interpreted as Obj2 calls the Obj1 method. Object Thisb calling Thisa's Functiona method
2. Understanding Obj2 If it is empty this points to window
1 Obj1.call (obj2,a,b); 2 // equivalent to 3 if (obj2!==null) {4     obj1.  this =obj2; 5     } Else {6     obj1.  this =window; 7 }8 obj1 (A, b);
3. Excerpt from the code in the JavaScript Advanced Program Book
1 var values = [1,2,3,4,5,6,9]; 2 var max = Math.max.apply (math,values); 3 console.log (max); // 9

Here math is used as the first parameter of apply () I can't understand, the Max () method is a method under the Math object, values are an array object, and you need to borrow the Max () method under the Math object.

1 var values = [1,2,3,4,5,6,9]; 2 var max = Math.max.apply (array,values); 3 console.log (max); // 9
1 var values = [1,2,3,4,5,6,9]; 2 var max = Math.max.apply (this, values); 3 console.log (max); // 9

Found using array or this instead of math altogether will output 9.

4. An example

Call and apply can be used to redefine the execution environment of a function, which is the point of this

1 function Changgestyle (attr,value) {2             this. style[attr] = value; 3         }4var box = document.getElementById ("box"); 5 window.changgeStyle.call (box, "height", "200px");

Changgestyle () is the method below the window object, so how to transfer the method of the Window object to the box object, by dynamically changing the direction of this, that is equivalent to borrowing Window.changgestyle () method, The call method turns Window.changgestyle ("height", "200px") into Box.changgestyle ("height", "200px").

Summary: Is the property or method that you do not have, by changing the execution environment of the function (this point), or the popular point is to borrow other people's properties or methods to perform the operation.

A simple understanding of the call () apply () method

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.