剛剛在一個羣裏看到有人問 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