让我们来看一下《JavaScript语言精粹》这本书中的一个例子:
Array.method('unshift', function () {
this.splice.apply(this,[0,0].concat(Array.prototype.slice.apply(arguments)));
return this;
});
为什么作者在某些地方使用了
this.splice
,而在另一些地方使用了Array.prototype.slice
?我曾尝试交换
this
和Array.prototype
,但是出现了以下错误:
TypeError: Cannot read property 'slice' of undefined
但我仍然不确定如何知道何时应该使用this
或Array.prototype
。