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

说一下对bind,call,apply三个函数的认识,自己实现一下bind方法 #9

Open
FunnyZ opened this issue Jul 16, 2019 · 3 comments
Labels
js javascript

Comments

@FunnyZ
Copy link
Collaborator

FunnyZ commented Jul 16, 2019

说一下对bind,call,apply三个函数的认识,自己实现一下bind方法

@artdong artdong added the js javascript label Jul 16, 2019
@FunnyZ
Copy link
Collaborator Author

FunnyZ commented Jul 17, 2019

粗略讲一下,希望大佬们能补充下。

首先这三个方法都是用来改变函数的 this 的绑定(指向)的。
它们的用法如下:

func.apply(thisArg, [argsArray])

fun.call(thisArg, arg1, arg2, ...)

function.bind(thisArg[, arg1[, arg2[, ...]]])

区别:

  • call 和 apply 的区别在于传参的形式不一样,apply 的参数形式是数组或类数组对象,call 的参数形式则是一个个排列的参数值;

  • bind 返回的是原函数的拷贝,并拥有指定的 this 值和初始参数;而 call 和 apply 都是直接返回原函数的返回值,或 undefined;即 bind 是需要手动去调用的,而 apply 和 call 都是立即自动执行。

实现 bind 方法可以参考 MDN bind polyfill

或者

const bind = (fn, context, ...boundArgs) => (...args) => fn.apply(context, [...boundArgs, ...args]);

@artdong artdong added this to done in fe-interview Jul 26, 2019
@hulkyuan
Copy link

hulkyuan commented Oct 12, 2019

有一个疑惑:
var obj=[2,3,7,3,8,1];
Math.max.apply(null,obj);//output 8
Math.max(obj);//output NaN
为何使用了apply后,第二个参数却有了展开语法的效果?难道使用apply后参数会被展开?
我能理解Math.max(...obj),但是apply明明传入的是数组,和Math.max要求的参数不一致呀

@artdong
Copy link
Collaborator

artdong commented Jul 9, 2020

模拟Math.max()

function max() {
    let max = arguments[0];
    for(let i = 0; i < arguments.length; i++) {
        if(max < arguments[i]) {
            max = arguments[i];
        }
    }
    return max;
}

max(...obj) // 8
max.call(null, ...obj); // 8
max.apply(null,obj) // 8 apply内部实现也有参数展开这一步

@hulkyuan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js javascript
Projects
Development

No branches or pull requests

3 participants