Javascript call() & apply() vs bind()?

后端 未结 22 2300
醉话见心
醉话见心 2020-11-22 02:42

I already know that apply and call are similar functions which setthis (context of a function).

The difference is with the way

22条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 03:20

    call/apply executes function immediately:

    func.call(context, arguments);
    func.apply(context, [argument1,argument2,..]);
    

    bind doesn't execute function immediately, but returns wrapped apply function (for later execution):

    function bind(func, context) {
        return function() {
            return func.apply(context, arguments);
        };
    }
    

提交回复
热议问题