Skip to content

Types: add new type array-like objects#710

Closed
arthurvr wants to merge 1 commit into
jquery:masterfrom
arthurvr:arrayLikes
Closed

Types: add new type array-like objects#710
arthurvr wants to merge 1 commit into
jquery:masterfrom
arthurvr:arrayLikes

Conversation

@arthurvr
Copy link
Copy Markdown
Member

@arthurvr arthurvr commented Apr 9, 2015

Fixes #708.

Comment thread pages/Types.html Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ); // 1

since "test" property is also "content" of the array-like object.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I remembered it backwards. Our definition checks for non-negative but I couldn't find that in the spec.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ); // undefined

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think by quoting @gibson042 you can't go wrong. 😄

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 😉

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And no one notice the guy who initially point that out :-)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That suggestion seems like reasonable @gibson042! Thanks y'all! I'll update the PR.

@arthurvr
Copy link
Copy Markdown
Member Author

Well, what do we think about this version?

@gibson042
Copy link
Copy Markdown
Member

👍

Also, ❤️ @markelog for identifying both the problem and the solution. 😄

@dmethvin
Copy link
Copy Markdown
Member

Just coming in to say that I ❤️ @markelog too. Oh and the PR looks good too 😸

@arthurvr
Copy link
Copy Markdown
Member Author

❤️ 💓 @markelog 😻 💟

@arthurvr arthurvr closed this in 929f9e2 Apr 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Create an "Array-Like" type

5 participants