Function.prototyoe.call.apply

刚刚在一个群里看到有人问 Function.prototype.call.apply(fun, args) 如何理解,觉得挺有意思的。刚开始被惯性思维干扰了,一直都是 call 和 apply 分开用呀!!!

如果理解 [].slice.apply(fun, args) 的话,也就能理解 Function.prototype.call.apply(fun, args) 了。

假设 fun = function(a){console.log(a)};

由于“.”的操作顺序是从右到左,所以

(1)首先以最右边的“.”为分割点,左边的 fun1=Function.prototype.call 为一个整体,调用 fun1.apply(fun, args);

(2)作用域替换成 fun之后变成,就变成了 fun.call(args[0], args[1], ...);

(3)再次替换作用域,fun 的 this 指向 args[0],a = args[1];

就这样啦

原文地址:https://www.cnblogs.com/xxhuan/p/7004421.html