jQuery: The Write Less, Do More JavaScript Library

$(0) inconsistent with $(1), $(2)

View: New views
8 Messages — Rating Filter:   Alert me  

$(0) inconsistent with $(1), $(2)

by Xavier Shay-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> $(0).get(0)
Document
> $(1).get(0)
1
> $(2).get(0)
2

Why does $(0) return a document? Is this intended behaviour? I would
expect it to return 0.

--

You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@....
To unsubscribe from this group, send email to jquery-dev+unsubscribe@....
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en.



Re: $(0) inconsistent with $(1), $(2)

by John Resig :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This changed slightly in the latest nightlies - if you do $(0) it'll
be as if you did $() or $([]). As of now any false-ish value will give
you an empty jQuery set.

Considering that there is no intended behavior for passing in a number
to the jQuery object this seems fine to me.

I'm curious - why were you passing in a number to the jQuery object? I
could sort of, kind of, understand passing in an array of values - but
just one number doesn't make sense.

--John



On Sat, Oct 31, 2009 at 9:16 PM, Xavier Shay <xavier.shay@...> wrote:

>> $(0).get(0)
> Document
>> $(1).get(0)
> 1
>> $(2).get(0)
> 2
>
> Why does $(0) return a document? Is this intended behaviour? I would
> expect it to return 0.
>
> --
>
> You received this message because you are subscribed to the Google Groups "jQuery Development" group.
> To post to this group, send email to jquery-dev@....
> To unsubscribe from this group, send email to jquery-dev+unsubscribe@....
> For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en.
>
>
>

--

You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@....
To unsubscribe from this group, send email to jquery-dev+unsubscribe@....
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en.



Re: $(0) inconsistent with $(1), $(2)

by dave.methvin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Why does $(0) return a document? Is this intended behaviour?

In 1.3.2, $(anything_falsy) returns the document. In the nightlies, $
(anything_falsy) returns an empty jQuery object. It was changed
because $(undefined) returned the document and that could lead to some
hard-to-debug situations.

> I would expect it to return 0.

The docs don't say what happens when you pass a number to $(), so it's
not an expected argument to the constructor.

http://docs.jquery.com/Core/jQuery#expressioncontext

"This function accepts a string containing a CSS selector..."

Did you find this by accident, or are you writing code that depends on
$(2) returning 2?

--

You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@....
To unsubscribe from this group, send email to jquery-dev+unsubscribe@....
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en.



Re: $(0) inconsistent with $(1), $(2)

by Xavier Shay-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Nov 2, 1:28 am, John Resig <jere...@...> wrote:

> This changed slightly in the latest nightlies - if you do $(0) it'll
> be as if you did $() or $([]). As of now any false-ish value will give
> you an empty jQuery set.
>
> Considering that there is no intended behavior for passing in a number
> to the jQuery object this seems fine to me.
>
> I'm curious - why were you passing in a number to the jQuery object? I
> could sort of, kind of, understand passing in an array of values - but
> just one number doesn't make sense.
It was an array of values, but that array could also be a number,
which was then converted into an array, except for the 0 case.
This is bad form, relying on undocumented behaviour. I have since
fixed my code, but still curious.
Principle of least surprise says that $(1) and $(0) should be
consistent. To match the nightlies, perhaps $(1) should be an empty
jQuery object?

> Did you find this by accident, or are you writing code that depends on
> $(2) returning 2?
By accident, found that $(2) returns [2], so using it as a conversion
(though no longer, as above)

Xavier

--

You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@....
To unsubscribe from this group, send email to jquery-dev+unsubscribe@....
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en.



Re: Re: $(0) inconsistent with $(1), $(2)

by Erik Beeson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Depends on what surprising you I guess. Truthy and falsy values returning different things might not be particularly surprising...

--Erik


On Mon, Nov 2, 2009 at 2:03 PM, Xavier Shay <xavier.shay@...> wrote:
Principle of least surprise says that $(1) and $(0) should be
consistent. To match the nightlies, perhaps $(1) should be an empty
jQuery object?

--

You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@....
To unsubscribe from this group, send email to jquery-dev+unsubscribe@....
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en.

Re: Re: $(0) inconsistent with $(1), $(2)

by Michael Geary-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Remember that jQuery is a DOM library, not a general purpose JavaScript utility library. When the thing you're doing is unrelated to the DOM, don't look for the solution in jQuery. It probably won't be there, or if it appears to be there, it will probably surprise you because it was coded specifically for DOM use. Either code your own solution or find one that somebody has coded.

If I understand what you're trying to do, what you're looking for is [].concat(...). For example:

console.log( [].concat(0) );  // [0]
console.log( [].concat(1) );  // [1]
console.log( [].concat([2,3]) );  // [2,3]

Or you can make that into a function:

function copyArray() {
    return Array.prototype.concat.apply( [], arguments );
}

console.log( copyArray(0) );  // [0]
console.log( copyArray(1) );  // [1]
console.log( copyArray([2,3]) );  // [2,3]

-Mike

On Mon, Nov 2, 2009 at 2:03 PM, Xavier Shay <xavier.shay@...> wrote:


On Nov 2, 1:28 am, John Resig <jere...@...> wrote:
> This changed slightly in the latest nightlies - if you do $(0) it'll
> be as if you did $() or $([]). As of now any false-ish value will give
> you an empty jQuery set.
>
> Considering that there is no intended behavior for passing in a number
> to the jQuery object this seems fine to me.
>
> I'm curious - why were you passing in a number to the jQuery object? I
> could sort of, kind of, understand passing in an array of values - but
> just one number doesn't make sense.
It was an array of values, but that array could also be a number,
which was then converted into an array, except for the 0 case.
This is bad form, relying on undocumented behaviour. I have since
fixed my code, but still curious.
Principle of least surprise says that $(1) and $(0) should be
consistent. To match the nightlies, perhaps $(1) should be an empty
jQuery object?

> Did you find this by accident, or are you writing code that depends on
> $(2) returning 2?
By accident, found that $(2) returns [2], so using it as a conversion
(though no longer, as above)

Xavier

--

You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@....
To unsubscribe from this group, send email to jquery-dev%2Bunsubscribe@....
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en.



--

You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@....
To unsubscribe from this group, send email to jquery-dev+unsubscribe@....
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en.

Re: Re: $(0) inconsistent with $(1), $(2)

by Nick Fitzsimons :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/11/2 Xavier Shay <xavier.shay@...>:
>
> Principle of least surprise says that $(1) and $(0) should be
> consistent. To match the nightlies, perhaps $(1) should be an empty
> jQuery object?
>

To my mind, the "principle of least surprise" suggests that jQuery
should throw an exception when passed something for which its
behaviour is undefined, like a number. It shouldn't be particularly
surprising to see an error when one starts throwing invalid input at a
function, and it means that people won't fall into the trap of finding
some undocumented and unintended behaviour and starting to rely on it.

Regards,

Nick.
--
Nick Fitzsimons
http://www.nickfitz.co.uk/

--

You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@....
To unsubscribe from this group, send email to jquery-dev+unsubscribe@....
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en.



Re: $(0) inconsistent with $(1), $(2)

by Robert Katić :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Well if $() is same as $([]), then instead of

if ( !selector ) {
  return this;
}

in $.fn.init, we can put

if ( selector == null ) {
  return this;
}

This would eliminate the $(0) "unexpected" behavior.

On Nov 1, 3:28 pm, John Resig <jere...@...> wrote:

> This changed slightly in the latest nightlies - if you do $(0) it'll
> be as if you did $() or $([]). As of now any false-ish value will give
> you an empty jQuery set.
>
> Considering that there is no intended behavior for passing in a number
> to the jQuery object this seems fine to me.
>
> I'm curious - why were you passing in a number to the jQuery object? I
> could sort of, kind of, understand passing in an array of values - but
> just one number doesn't make sense.
>
> --John
>
> On Sat, Oct 31, 2009 at 9:16 PM, Xavier Shay <xavier.s...@...> wrote:
> >> $(0).get(0)
> > Document
> >> $(1).get(0)
> > 1
> >> $(2).get(0)
> > 2
>
> > Why does $(0) return a document? Is this intended behaviour? I would
> > expect it to return 0.
>
> > --
>
> > You received this message because you are subscribed to the Google Groups "jQuery Development" group.
> > To post to this group, send email to jquery-dev@....
> > To unsubscribe from this group, send email to jquery-dev+unsubscribe@....
> > For more options, visit this group athttp://groups.google.com/group/jquery-dev?hl=en.

--

You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@....
To unsubscribe from this group, send email to jquery-dev+unsubscribe@....
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en.