Javascript method difference between apply and call

Source: Internet
Author: User

Call and aplly are all part of a function.prototype approach, so each function instance has a

Same point:
The call () method and the Apply () method have the same effect:

改变原调用函数的内部this指向。

call()方法和apply()方法的第一个參数都是调用call/apply方法的函数内部this的指向。

different points:
When the function that calls the Call/apply method has a parameter, the Call/apply method also needs to be passed

call的2參、3參...n參分别表示原函数的1參、2參...n-1參apply的2參是一个数组,数组元素按顺序存储原方法的1參、2參...n參

Example:

function add(c, d){/*    这里的this表示add.call/apply的一參    假设直接调用add(c,d),那么this指向window*/returnthisthisvar o = {a:1, b:357// 1 + 3 + 5 + 7 = 16 add.apply(o, [1020// 1 + 3 + 10 + 20 = 34 

Javascript method difference between apply and call

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.