JS bind & apply/call

发布时间 2023-06-10 19:04:09作者: sqmw

bind

let boundFunc = func.bind(context);
  • 将func的context修改为传入的参数,返回一个新的func函数

call

func.call(context, arg1, arg2, ...)

apply

func.apply(context, args)
  • call 和 apply 之间唯一的语法区别是,call 期望一个参数列表,而 apply 期望一个包含这些参数的类数组对象。
  • func.call(context, ...args); === func.apply(context, args);