Function.prototype.apply () vs. Function.prototype.call () in JavaScript

Source: Internet
Author: User

Function.prototype.apply () | The Function.prototype.call () apply () method can invoke a function or method with a specified this value and a parameter array (or class array object). The call () method is similar to apply (), and the only difference is that the parameter that is accepted by call () is the parameter list.
Nutshell:
Apply () A This, a parameter
Call () A This, multiple parameters
Grammar
Fun.apply (thisarg[, Argsarray]) |fun.call (thisarg[, arg1[, arg2[, ...])
Parameters
Apply (): Thisarg: The This value that is specified when the fun function is run. Note that the specified this value is not necessarily the true this value when the function executes, and if the function is in non-strict mode, the this value, specified as null and undefined, is automatically directed to the global object (which is the Window object in the browser), and the value is the original value (number , String, Boolean) This will point to the automatic wrapper object for that original value. Argsarray: Array (or class array object) Call (): Thisarg: The parameter description of the same apply (). Arg1[,arg2[,arg3[...]]: parameter list
This article original blog address: http://www.cnblogs.com/unofficial official website address: www.pushself.com DEMO
Apply () Demofunction Siteinfo (name,site) {this.name = Name;this.site = Site;} function Contactme (NAME,SITE,QQ) {//siteinfo.apply (this, {0:name, 1:site, length:2});//siteinfo.apply (this, [name, Site]); Siteinfo.apply (this, arguments); this.qq = QQ;} var contactme = new Contactme ("unofficial", "www.pushself.com", "1936**3***"); Console.log ("Hello, I Am" +contactme.name+ ", Welcome to visit "+contactme.site+", there are questions can leave a message, also can through QQ: "+contactme.qq+" timely contact Me ");
Here is the application of apply (), where the main note is the Argsarray parameter, which can be an array or an array of classes object.
The arguments object is a local variable provided in all methods and cannot be used in the prototype property of a method. It is necessary to note that arguments is not an array, just an array of classes.
Call () demofunction Siteinfo (name,site) {this.name = Name;this.site = Site;} function Contactme (NAME,SITE,QQ) {Siteinfo.call (this, name, site); this.qq = QQ;} var contactme = new Contactme ("unofficial", "www.pushself.com", "1936**3***"); Console.log ("Hello, I Am" +contactme.name+ ", Welcome to visit "+contactme.site+", there are questions can leave a message, also can through QQ: "+contactme.qq+" timely contact Me ");
Here is the application of call (), where it is important to note that the second parameter is a list of many parameters.
This article original blog address: http://www.cnblogs.com/unofficial official website address: www.pushself.com Extended demo can use apply () to find the maximum value in the array, for example, I asked for an array var arr = [19,360,0,3000];
var arr = [19,360,0,3000],    maxval = Math.max.apply (null, arr),    minval = Math.min.apply (null, arr); Console.log (" The maximum value in arr is: "+maxval+" and the minimum value is: "+minval);
This is using apply () to find the maximum value in the array, usually we use Math.max () to find the maximum value of a list, for example: Math.max (19,360,0,3000), the maximum value is 3000, if this is an array or an array object, How do we get the maximum value of it? is to use apply () to solve, Math.max.apply (null, arr) equivalent to the This object is Math.max, the arr after apply () processed into a list, and finally the maximum value, or the minimum value.
This article original blog address: http://www.cnblogs.com/unofficial official website address: www.pushself.com Reference Mozilla Developer Community: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
Mozilla Developer community: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
This article original blog address: http://www.cnblogs.com/unofficial official website address:www.pushself.com

Function.prototype.apply () vs. Function.prototype.call () 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.