刚刚在一个群里看到有人问 Function.prototype.call.apply(fun, args) 如何理解,以为挺有意思的。刚开始被惯性思惟干扰了,一直都是 call 和 apply 分开用呀!!!app
若是理解 [].slice.apply(fun, args) 的话,也就能理解 Function.prototype.call.apply(fun, args) 了。this
假设 fun = function(a){console.log(a)};prototype
因为“.”的操做顺序是从右到左,因此作用域
(1)首先以最右边的“.”为分割点,左边的 fun1=Function.prototype.call 为一个总体,调用 fun1.apply(fun, args);io
(2)做用域替换成 fun以后变成,就变成了 fun.call(args[0], args[1], ...);console
(3)再次替换做用域,fun 的 this 指向 args[0],a = args[1];function
就这样啦call