I’ve got an Uint8Array on which I want to use the slice method.
So I took advantage of the array slice method like this:
Array.prototype.slice.call(array, start, end);
Now I want to outsource some common used functions like this.
I tried it with
var utils = {
slice: function() {
return Array.prototype.slice.apply(this, arguments);
}
};
But apparently I misunderstood how it really works. Can anybody explain this to me and how to achieve it to get working?
Your problem lies in that
thisis not what you think it is. If you callutils.slice(foo, 1, 2),thiswill beutils.Depending on how you want to call it, you could pass the object you want to operate on as first argument, then you would do:
Usage:
Another (perhaps clearer) option is to just use named arguments:
This will work the same way.
Update: I got curious why
Uint8Arraydoesn’t haveslice, and I found out aboutsubarray:Note that
It might be the case that this is what you want — I’m betting on it being a lot more efficient — if you don’t need to copy the data, that is!