Types: add new type array-like objects#710
Conversation
There was a problem hiding this comment.
We could a more strict here, not only length property should be in such object, but also their other properties names (but not necessary all properties) should be numeric too and they should corresponds with length.
Personally, i like Axel Rauschmayer definition - http://www.2ality.com/2013/05/quirk-array-like-objects.html
There was a problem hiding this comment.
We don't enforce those extra rules though. A few days ago I read that ES6 defined "arraylike" as having a non-negative length value, which would be easy to enforce. However, I can't find any reference to that in the spec.
There was a problem hiding this comment.
We can't iterate on non-numeric properties, whereas merge docs says:
Merge the contents of two arrays together into the first array.
if there would be array-like there, it should, according to this definition of array-like do this:
var a = { test: 1 };
a.length = 1;
console.log( $.merge( [], a ).test ); // 1since "test" property is also "content" of the array-like object.
There was a problem hiding this comment.
Our de facto definition, which I observe agrees very well with Axel Rauschmayer's, is a nonnegative numeric length and a property corresponding to the highest index: https://github.com/jquery/jquery/blob/2380028ec4a6a77401b867a51de26a3cb8e8d311/src/core.js#L434-L448
There was a problem hiding this comment.
Oh, I remembered it backwards. Our definition checks for non-negative but I couldn't find that in the spec.
There was a problem hiding this comment.
So, what's the consensus in here?
Our de facto definition, which I observe agrees very well with Axel Rauschmayer's, is a nonnegative numeric length and a property corresponding to the highest index:
Using exactly that definition would suggest something like this works, isn't it?
var obj = { test: 3, 1: 4 };
obj.length = 2;
console.log( isArraylike(obj) ); // true
console.log( $.merge( [], obj ).test ); // undefinedThere was a problem hiding this comment.
Yes I think by quoting @gibson042 you can't go wrong. 😄
There was a problem hiding this comment.
I'd like this to be something like "an Object that contains a nonnegative integer length property and index properties from 0 up to length - 1". It describes the density that we assume (but are unwilling to fully verify at this point), and hopefully is clear enough to unambiguously include { length: 0 }.
Yes I think by quoting @gibson042 you can't go wrong.
I may hold you to that 😉
There was a problem hiding this comment.
And no one notice the guy who initially point that out :-)
There was a problem hiding this comment.
That suggestion seems like reasonable @gibson042! Thanks y'all! I'll update the PR.
|
Well, what do we think about this version? |
|
👍 Also, ❤️ @markelog for identifying both the problem and the solution. 😄 |
|
Just coming in to say that I ❤️ @markelog too. Oh and the PR looks good too 😸 |
|
❤️ 💓 @markelog 😻 💟 |
Fixes #708.