在看Javascript 秘密花园的时候,发现一段晦涩难懂的代码片段,由于 中文版的没有讲到这个技巧的妙处,所以硬着头皮,看看英文版
Another trick is to use both
call
andapply
together to turn methods - functions that use the value of this as well as their arguments - into normal functions which only use their arguments.
(大概的意思是:可以一起使用call
和apply
将使用this
和参数的方法函数转化为使用相同参数的普通函数),
这不是在普通函数中利用call
和apply
,绑定传入的上下文吗?嗯...挺类似的,但是它两个一起使用,因为上下文是一个函数,需要再利用一次。
全部代码如下