Let's take this example from The Good Parts
book:
Array.method('unshift', function () {
this.splice.apply(this,[0,0].concat(Array.prototype.slice.apply(arguments)));
return this;
});
Why did the author use this.splice
in one place and Array.prototype.slice
in other?
I tried swapping out this
and Array.prototype
with each other and got errors like the following:
TypeError: Cannot read property 'slice' of undefined
but I am still not sure about, how to know when to should use this
or Array.prototype
.