$() with more than one argument
Reported by Douwe M. | February 16th, 2010 @ 11:25 AM
Allow us to use the $()
function with more than one
argument, so $('foo', 'bar', 'baz')
would return an
array, the same as $$('#foo, #bar, #baz')
.
Comments and changes to this ticket
-
MadRabbit February 16th, 2010 @ 06:03 PM
the
$
method is a fast version of$$('#foo')[0]
, it was specifically made to deal with the majority of cases, when you refer to just one node. So you kinda offer to reverse the optimization backward, coz this feature will slow the method down.and then, IMHO
$$('#foo, #boo, #hoo')
is more readable and expressive, than the version with several arguments. -
Douwe M. February 16th, 2010 @ 06:12 PM
Well,
$()
with multiplearguments
would just have to loop through thearguments
, and rundocument.getElementById()
on every one of them. That would only bearguments.length
times as slow as$()
with only oneargument
, and it won't be (noticeably) slower when using only oneargument
, like it works now.
IMO it makes sense to use$()
when picking an elementid
, and using$$()
when I want to use an often more complex CSS selector. -
MadRabbit February 16th, 2010 @ 06:28 PM
I understand your point, bur the
$()
method is not just finds an element by an id, it also extends elements under IE7,6 browsers, and it will also be used to access elements functionality when it will come to the safe-mode implementation. Your changes might have a small impact by itself, but as the method is called repeatedly in many places, it is crucial to keep it as small and fast as possible.Here. You can do the trick in a tough way like that. :)
$w('foo boo hoo').map($);
-
Douwe M. February 16th, 2010 @ 06:32 PM
But if
$()
would just loop through thearguments
, and return one node whenarguments.length == 1
, and an array when it's more, the only extra code would be a loop that wouldn't even be used in most cases, and a check for thearguments.length
... I can see your point, but I just don't really see how this would slow it down so much...Haha, nice one. I think I'll just continue using
$$('#foo, #bar, #baz')
then :) -
MadRabbit February 16th, 2010 @ 07:04 PM
- State changed from new to invalid
because the complexity of algorithms gets multiplied, visiting every element in an array has a linear complexity O(n), visiting every item in an array of arrays, is square O(n^2). Changing one item processing to an array, might not a big deal for a simple function, especially if you deal with just a few items, but it will shift the complexity level for another algorithms that use this method.
if you need this feature badly, RightJS is an open-box and you always can redefine the method for your app with just a couple of lines of code.
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป
RightJS Core Tickets