How to get the reference to a slice of an array?
var A = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
A.mySlice = function(l, h){return this.slice(l,h)};
var B = A.mySlice(1,5); // ["b", "c", "d", "e"]
It works for direct slices derived from A. But, how to get it for all slices derived? (in this case for B)
B.mySlice = function(l, h){return this.slice(l,h)};
A[3] = 33;
A.mySlice(1,5) // ["b", "c", 33, "e"] => ok
B.mySlice(0,3) // ["b", "c", "d"] => I would want ["b", "c", 33]
I don’t think you can do this with native JS arrays (well, not in a straightforward manner anyway).
I think the cleanest approach would be going back and using custom objects to represent the slices. Perhaps something along these lines:
Of course, this loses the convenient
[]syntax and the objects aren’t arrays anymore but subclassing Array is annoying and error prone and I don’t believe there is a cross-browser way to do operator overloading.