Skip to content

Conversation

@mr21
Copy link
Contributor

@mr21 mr21 commented May 31, 2015

Hi :)

I found in JS we have this:

var n = 0;
n.length === undefined;
false.length === undefined;
true.length === undefined;
NaN.length === undefined;

// There is an exception here:
undefined.length; // crash

And with the actual jQuery, sometime we have to do this:

if (obj)
    $.each(obj, function() {
        // ...
    });

If we reorder few instructions in the $.each function we can remove the necessity of the if above.

Do you agree?

EDIT (by @mzgol): Fixes gh-2267

@timmywil
Copy link
Member

timmywil commented Jun 1, 2015

Thank you for opening a PR. We've discussed this issue here. It also affects .map. Would you be up for opening a PR for that too? I think .each and .map do need adjustments, since they check length before the call to isArraylike, but if it would save anything to make changes there as well, feel free.

@mgol
Copy link
Member

mgol commented Jun 1, 2015

Such a change requires a test, though.

@mr21
Copy link
Contributor Author

mr21 commented Jun 1, 2015

Oh right, cool, I will do this (the other PR and the test), this evening.
Thanks :)

@mr21
Copy link
Contributor Author

mr21 commented Jun 2, 2015

I added a test, but this test fail actually, because modifying $.each is not enough... I have to reorder the isArraylike function, I made this change here #2368.
Maybe you can merge the .map PR before this one.

Copy link
Member

Choose a reason for hiding this comment

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

If this PR should fix #2267 you probably should add tests for all those values

@mgol
Copy link
Member

mgol commented Jun 2, 2015

@mr21 a PR should close an issue if it changes behavior so perhaps you could consolidate the changes in one PR that would fix #2267?

@markelog
Copy link
Member

markelog commented Jun 2, 2015

One PR is preferable, but we could do two atomic commits, one would ref, other would close

@timmywil
Copy link
Member

timmywil commented Jun 2, 2015

Sorry, I should have suggested one PR.
On Tue, Jun 2, 2015 at 08:57 Oleg Gaidarenko notifications@github.com
wrote:

One PR is preferable, but we could do two atomic commit, one would ref,
other would close


Reply to this email directly or view it on GitHub
#2363 (comment).

@mr21 mr21 force-pushed the each-undefined branch from 4f9cf25 to cc9c714 Compare June 2, 2015 23:54
@mr21
Copy link
Contributor Author

mr21 commented Jun 2, 2015

So, I closed the other PR about .map.
I restart this PR too, can you review this first (atomic) commit about the fix of isArraylike?

src/core.js Outdated
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here the code return false for: null, undefined, "number", NaN, "function", "window" only.

Copy link
Member

Choose a reason for hiding this comment

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

This is very hard to read. Please assign type in its declaration, and remove the type === "number" check completely. Also, it may be smaller to assign length in its declaration as well (e.g., length = obj != null && "length" in obj && obj.length).

@gibson042
Copy link
Member

This PR needs a reference to #2267 and unit tests for passing null/undefined to jQuery.each and jQuery.map (and corresponding changes to those methods so they don't throw on accessing .length).

@mr21
Copy link
Contributor Author

mr21 commented Jun 3, 2015

@gibson042, yeah I know I did it yesterday but markelog spoke about two atomic commits.
Have I to make only two commits here or it's you after who will reorganize all the code of the PR before the push on master?
It's the reason why I didn't add my unit test today, because I wanted to be sure about isArraylike.

@gibson042
Copy link
Member

Just add relevant commits for both jQuery.each and jQuery.map to this branch, which will close an updated #2267 when it lands. isArraylike will only need changes as necessary to support the newly-valid null and undefined input for those public methods.

@mr21 mr21 force-pushed the each-undefined branch from cc9c714 to 8d1db1d Compare June 7, 2015 20:35
@mr21
Copy link
Contributor Author

mr21 commented Jun 7, 2015

@gibson042 all is done :)

@mgol
Copy link
Member

mgol commented Jun 25, 2015

@mr21 Could you squash your commits and add Fixes gh-2267 to the commit message? I modified its title so that it doesn't mention numbers which are not officially handled. See d471842 for an example commit message.

@mr21 mr21 force-pushed the each-undefined branch from 8d1db1d to 9a77f72 Compare June 25, 2015 04:18
@mr21
Copy link
Contributor Author

mr21 commented Jun 25, 2015

@mzgol, right, it's done :)

Copy link
Member

Choose a reason for hiding this comment

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

You mention Function here but the test is only about undefined/null.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh it's because I am testing :

$.each( null || undefined, function() {
});

Like the title of the precedent test ( jQuery.each(Object,Function) )

Copy link
Member

Choose a reason for hiding this comment

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

Ah, it's a comma, not a slash. OK, sorry. :)

Choose a reason for hiding this comment

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

This landed and nobody noticed that it should have used expect( 1 ) instead of the deprecated argument =/

Copy link
Member

Choose a reason for hiding this comment

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

Oh the humanity! How can we possible recover from this horrible-horrible mistake?!
...
Sorry, couldn't resist.

We will fix all of that occurrences at some point.

@mgol mgol self-assigned this Jul 8, 2015
@mgol
Copy link
Member

mgol commented Jul 27, 2015

LGTM now.

@mgol mgol closed this in bf48c21 Jul 27, 2015
mgol pushed a commit that referenced this pull request Jul 27, 2015
@mgol
Copy link
Member

mgol commented Jul 27, 2015

Landed, thanks!

riichard pushed a commit to riichard/jquery that referenced this pull request Sep 20, 2015
@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

isArrayLike no longer fails gracefully for null and undefined from 1.11.1 to 1.11.3

7 participants