Definitions of the apply and call methods and differences between the apply and call Methods _ javascript tips-js tutorial

Source: Internet
Author: User
The apply and call functions are the same, but the input parameter lists are in different forms. This article introduces the definitions of the apply and call methods and the differences between the apply and call methods, if you are interested in learning it together, if you haven't touched any dynamic language, it will be amazing and strange to understand javaScript in the way of compiling language thinking, because it is often impossible to realize things happen, and even feel unreasonable. if you encounter this feeling when learning JavaScript, a free and ever-changing language, let go of your "prejudice" from now on ", this is definitely a new world for you. Let's get down to the truth. Let's explain the definitions of the apply and call methods first.

The specific content is as follows:

1. method definition

Call and apply all belong to Function. prototype is implemented internally by the JavaScript engine because it belongs to Function. prototype, so each Function object instance, that is, each method has the call and apply attribute. since they are attributes of methods, their use is of course for methods. these two methods are easy to confuse, because they have the same effect, but they are used in different ways.

Call method:

Syntax: call ([thisObj [, arg1 [, arg2 [, [,. argN])

Definition: call a method of an object to replace the current object with another object.

Note:

The call method can be used to call a method instead of another object. The call method can change the object context of a function from the initial context to the new object specified by thisObj.

If the thisObj parameter is not provided, the Global object is used as thisObj.

Apply method:

Syntax: apply ([thisObj [, argArray])

Definition: apply a method of an object and replace the current object with another object.

Note:

If argArray is not a valid array or an arguments object, a TypeError occurs.

If neither argArray nor thisObj is provided, the Global object will be used as thisObj and cannot be passed with any parameters.

Call, apply is called by someone else's method, just like calling your own.

Their differences:

Apply: a maximum of two parameters are allowed: The New this object and an array argArray. If multiple parameters are passed to this method, all parameters are written into this array. Of course, even if there is only one parameter, they must be written into the array. If argArray is not a valid array or an arguments object, a TypeError occurs. If neither argArray nor thisObj is provided, the Global object will be used as thisObj and cannot be passed with any parameters.

Call: this is a list of direct parameters. It is mainly used when the methods of js objects call each other to make the current pointer of this instance consistent, or in special cases, you need to change the this pointer. If the thisObj parameter is not provided, the Global object is used as thisObj.

To put it simply, the apply and call functions are the same, but the input parameter lists are in different forms: for example, the apply method corresponding to func. call (func1, var1, var2, var3) is as follows:

Func. apply (func1, [var1, var2, var3])

For example:

Add. apply (sub, [3, 1]); // add. call (sub, 3, 1); var a = {n: 1, m: 2, add: function () {return this. n + this. m ;}} var B = {n: 3, m: 4} console. log (. add. call (B); // B. n + B. m = 7 function Animal () {this. name = "Animal"; this. showName = function () {alert (this. name) ;}} function Cat () {this. name = "Cat";} var animal = new Animal (); var cat = new Cat (); // use the call or apply method to convert the showName () that originally belongs to the Animal object () the method is handed over to the object cat for use. // The input result is "Cat" animal. showName. call (cat, ","); animal. showName. apply (cat, []).

The above content is the difference between the definitions of the apply and call methods and the apply and call methods described in the editor. I hope you will like them.

Related Article

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.