Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] 46.call() 和 apply() 的含义和区别? #215

Open
qiilee opened this issue Sep 30, 2019 · 0 comments
Open

[js] 46.call() 和 apply() 的含义和区别? #215

qiilee opened this issue Sep 30, 2019 · 0 comments
Labels

Comments

@qiilee
Copy link
Member

qiilee commented Sep 30, 2019

答案:

首先说明两个方法的含义:

  • call:调用一个对象的一个方法,用另一个对象替换当前对象。例如:B.call(A, args1,args2);即 A 对象调用 B 对象的方法。
  • apply:调用一个对象的一个方法,用另一个对象替换当前对象。例如:B.apply(A, arguments);即 A 对象应用 B 对象的方法。

call 与 apply 的相同点:

  • 方法的含义是一样的,即方法功能是一样的;
  • 第一个参数的作用是一样的;

call 与 apply 的不同点:两者传入的列表形式不一样

  • call 可以传入多个参数;
  • apply 只能传入两个参数,所以其第二个参数往往是作为数组形式传入
@qiilee qiilee added the JS label Sep 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant