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]
slice()
copies elements to a new array.So, in your example,
A
andB
are two completely different arrays. So, changing elements in one does not affect another.Here is the only possible solution I see:
This may however break lots of your code.
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.It's not quite as clean, since you'd be dealing with function objects, but you could wrap the calls to chain back up the original array using closures:
I'm pretty sure this isn't possible in Javascript. This line
var B = A.mySlice(1,5);
sets B to an array object containting a slice of A's numbers. It has no reference to A. It's just an array object.