One JavaScript instance-apply and call every day, javascript-apply

Source: Internet
Author: User

One JavaScript instance-apply and call every day, javascript-apply

<! DOCTYPE html> 


What are the differences between the apply () and call () methods in javascript?

Call and apply all belong to Function. A prototype method is implemented internally by the JavaScript engine because it belongs to the 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.
Similarities: the two methods have the same effect.
Differences: parameters passed by methods are different.

Javascript call apply is not understood

Call and apply can be called using variables as function names. For example, the callback function of a function. The specific usage is: executed function. call (a, B, c ...), a is the object that this needs to be specified in the executed function. It can be null. Other parameters are used as parameters of the executed function. The usage of apply is similar, but the second parameter is an array.
Example:
Function doPost (url, param, callback ){
// Process post requests here
Var str = xhr. responseText;
Callback. apply (this, [str]); // It is equivalent to calling callback (str); and setting this in callback as a doPost object
}

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.