Comparison between Function. prototype. apply () and Function. prototype. call () in javascript

Source: Internet
Author: User

Comparison between Function. prototype. apply () and Function. prototype. call () in javascript
The apply () method can call a function or method while using a specified this value and a parameter array (or an array of classes. The call () method is similar to apply (). The difference is that the parameters accepted by call () are the parameter list. In short: apply () is a this, call () is a this parameter, and multiple parameter syntaxes are fun. apply (thisArg [, argsArray]) | fun. call (thisArg [, arg1 [, arg2 [,...]) parameter apply (): thisArg: this value specified when the fun function is running. It should be noted that the specified this value is not necessarily the true this value during function execution. If this function is in non-strict mode, the value of this specified as null and undefined automatically points to the Global Object (window object in the browser), and the value is the original value (number, String, Boolean value) this will point to the automatic packaging object of the original value. ArgsArray: array (or class array object) call (): thisArg: Same as the parameter description of apply. Arg1 [, arg2 [, arg3 […]: 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" + contactMe. site + ". If you have any questions, please stay You can also use QQ: "+ contactMe. qq + "contact me in time"); here is the application of apply (). The main note is the argsArray parameter, which can be an array or a class array object. The arguments object is a local variable provided by all methods. It cannot be used in the method's prototype attribute. It must be noted that arguments is not an array but 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" + contactMe. site + ". If you have any questions, you can leave a message or use QQ:" + contactMe. qq + "contact me in time"); here is the call () application. Note that the second parameter is a list composed of many parameters. Original blog address: http://www.cnblogs.com/unofficial official website address: www.pushself.com extension DEMO can use apply () to find the maximum value in the array, for example, I require an array var arr = [19,360,]; var arr = [19,360,], 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 the maximum value in the Array Using apply (). We usually use Math. max () is the maximum value of a list, for example, Math. max (19,360, 3000), the maximum value is. If this is an array or an array object, how can we obtain the maximum value? Is to use apply () to solve, Math. max. apply (null, arr) is equivalent to this object is Math. max. After applying (), arr is processed as a list, and the maximum or minimum value is obtained.

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.