Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Prototype: "element-id".$() instead of $('element-id')
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  20 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Dr Nic  
View profile  
 More options Sep 11 2006, 3:59 am
From: "Dr Nic" <drnicwilli...@gmail.com>
Date: Mon, 11 Sep 2006 07:59:26 -0000
Local: Mon, Sep 11 2006 3:59 am
Subject: Prototype: "element-id".$() instead of $('element-id')
The Prototype library gives us the $() operation for converting a DOM
element id into the DOM element: $('element-id'). It also appends a
bunch of functions to the resulting object.

Sometimes though, passing a string into the $() function doesn't read
well; and only makes Javascript code harder to read. For example:
$(window.button_list()[3]).hide()

Instead, it'd be nice to have normal chainability. I like the
following syntax: window.button_list()[3].$().hide().

That is, call the $() method on a string object, instead of passing the
string into the $() method.

Add this code into your application:

String.prototype.$ = function() {
  return $(document.getElementById(this));

}
>From article:

http://drnicwilliams.com/2006/09/11/prototype-call-dollar-on-string/

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christophe Porteneuve aka TDD  
View profile  
 More options Sep 11 2006, 5:51 am
From: Christophe Porteneuve aka TDD <t...@tddsworld.com>
Date: Mon, 11 Sep 2006 11:51:48 +0200
Local: Mon, Sep 11 2006 5:51 am
Subject: Re: [Rails-spinoffs] Prototype: "element-id".$() instead of $('element-id')
Hi,

Dr Nic a écrit :

> Sometimes though, passing a string into the $() function doesn't read
> well; and only makes Javascript code harder to read. For example:
> $(window.button_list()[3]).hide()

Actually, this is widely acclaimed as a nice way to do it.  Of course,
there's no accounting for taste, but it is nicely unobstrusive, which is
the whole point of choosing such a short name.

> Instead, it'd be nice to have normal chainability. I like the
> following syntax: window.button_list()[3].$().hide().

Suit your taste, it's pretty easy to do so.  But I don't think I dare
too much in asserting this will *not* happen in the official codebase.

--
Christophe Porteneuve aka TDD
t...@tddsworld.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nic Williams  
View profile  
 More options Sep 11 2006, 6:24 am
From: "Nic Williams" <drnicwilli...@gmail.com>
Date: Mon, 11 Sep 2006 12:24:10 +0200
Local: Mon, Sep 11 2006 6:24 am
Subject: Re: [Rails-spinoffs] Re: Prototype: "element-id".$() instead of $('element-id')

I agree, and I still use the $('element-id') version in most places. I found
'element-id'.$() reads well within a line of complex code where it becomes
less obvious what the $(...) is enveloping.

Cheers
Nic

On 9/11/06, Christophe Porteneuve aka TDD <t...@tddsworld.com> wrote:

--
Dr Nic Williams
http://www.drnicwilliams.com - Ruby/Rails blog
skype: nicwilliams
(m) +31 62 494 8552
(p) +61 7 3102 3237 (finds me anywhere in the world)
(f) +61 7 3305 7572 (sends fax to my email)

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Fuchs  
View profile  
 More options Sep 11 2006, 9:30 am
From: Thomas Fuchs <t.fu...@wollzelle.com>
Date: Mon, 11 Sep 2006 15:30:28 +0200
Local: Mon, Sep 11 2006 9:30 am
Subject: Re: [Rails-spinoffs] Prototype: "element-id".$() instead of $('element-id')
Why not write that as

String.prototype.$ = function() {
   return $(this);

}

?

-Thomas

Am 11.09.2006 um 09:59 schrieb Dr Nic:

--
Thomas Fuchs
wollzelle

http://www.wollzelle.com

questentier on AIM
madrobby on irc.freenode.net

http://www.fluxiom.com :: online digital asset management
http://script.aculo.us :: Web 2.0 JavaScript
http://mir.aculo.us :: Where no web developer has gone before


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nic Williams  
View profile  
 More options Sep 11 2006, 9:50 am
From: "Nic Williams" <drnicwilli...@gmail.com>
Date: Mon, 11 Sep 2006 15:50:49 +0200
Local: Mon, Sep 11 2006 9:50 am
Subject: Re: [Rails-spinoffs] Re: Prototype: "element-id".$() instead of $('element-id')

It didn't seem to work for me when I tried it:

In Firebug:

>>> "element-id".$()

["e","l","e","m","e","n","t","-","i","d"]

Any ideas why $(this) on a string defaults to splitting the string into
chars?

On 9/11/06, Thomas Fuchs <t.fu...@wollzelle.com> wrote:

--
Dr Nic Williams
http://www.drnicwilliams.com - Ruby/Rails blog
skype: nicwilliams
(m) +31 62 494 8552
(p) +61 7 3102 3237 (finds me anywhere in the world)
(f) +61 7 3305 7572 (sends fax to my email)

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Fuchs  
View profile  
 More options Sep 11 2006, 10:39 am
From: Thomas Fuchs <t.fu...@wollzelle.com>
Date: Mon, 11 Sep 2006 16:39:01 +0200
Local: Mon, Sep 11 2006 10:39 am
Subject: Re: [Rails-spinoffs] Re: Prototype: "element-id".$() instead of $('element-id')

Good question.

 >>> function $(){ return arguments[0]; }
 >>> String.prototype.$ = function() { return $(this) }
function () {...}
 >>> "blah".$()
["b","l","a","h"]
 >>> function $(){ return arguments[0].toUpperCase(); }
 >>> "blah".$()
"BLAH"

Anyway, no time to work this out,
Thomas

Am 11.09.2006 um 15:50 schrieb Nic Williams:

--
Thomas Fuchs
wollzelle

http://www.wollzelle.com

questentier on AIM
madrobby on irc.freenode.net

http://www.fluxiom.com :: online digital asset management
http://script.aculo.us :: Web 2.0 JavaScript
http://mir.aculo.us :: Where no web developer has gone before


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Lear  
View profile  
 More options Sep 11 2006, 1:12 pm
From: Chris Lear <chris.l...@laculine.com>
Date: Mon, 11 Sep 2006 18:12:59 +0100
Local: Mon, Sep 11 2006 1:12 pm
Subject: Re: [Rails-spinoffs] Re: Prototype: "element-id".$() instead of $('element-id')
* Thomas Fuchs wrote (11/09/06 15:39):

"string"

Not sure what this proves, except that the function returns a thing with
a type "object" rather than type "string". The way firebug displays
things probably depends on the result of typeof, and arrays give
"object", so Firebug thinks it's an array. Or something like that.

Chris


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nic Williams  
View profile  
 More options Sep 11 2006, 1:25 pm
From: "Nic Williams" <drnicwilli...@gmail.com>
Date: Mon, 11 Sep 2006 19:25:14 +0200
Local: Mon, Sep 11 2006 1:25 pm
Subject: Re: [Rails-spinoffs] Re: Prototype: "element-id".$() instead of $('element-id')

So String(this) works.

String.prototype.$ = function() {
  return $(String(this))

}
>>> "map".$()

<div id="map">

Thanks guys.
Cheers
Nic

On 9/11/06, Chris Lear <chris.l...@laculine.com> wrote:

--
Dr Nic Williams
http://www.drnicwilliams.com - Ruby/Rails blog
skype: nicwilliams
(m) +31 62 494 8552
(p) +61 7 3102 3237 (finds me anywhere in the world)
(f) +61 7 3305 7572 (sends fax to my email)

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christophe Porteneuve  
View profile  
 More options Sep 11 2006, 2:13 pm
From: Christophe Porteneuve <t...@tddsworld.com>
Date: Mon, 11 Sep 2006 20:13:49 +0200
Local: Mon, Sep 11 2006 2:13 pm
Subject: Re: [Rails-spinoffs] Re: Prototype: "element-id".$() instead of $('element-id')
Nic Williams a écrit :

> So String(this) works.

> String.prototype.$ = function() {
>   return $(String(this))
> }

>>>> "map".$()
> <div id="map">

Yeah, the issue with your original implementation is due to this test in
Prototype's $:

  if (typeof element == "string")

When you refer to yourself, as a String, using this, typeof says
"object".  A more reference-proof way of testing whether you actually
are a string would be to say:

  if (element.constructor === String)

Using this:

function $() {
  var results = [], element;
  for (var i = 0; i < arguments.length; i++) {
    element = arguments[i];
    if (element.constructor === String) {
      element = document.getElementById(element);
    }
    results.push(Element.extend(element));
  }
  return results.reduce();

}

Your original implementation:

  String.prototype.$ = function() { return $(this); }

works well too.

--
Christophe Porteneuve a.k.a. TDD
"[They] did not know it was impossible, so they did it." --Mark Twain
Email: t...@tddsworld.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nic Williams  
View profile  
 More options Sep 11 2006, 3:28 pm
From: "Nic Williams" <drnicwilli...@gmail.com>
Date: Mon, 11 Sep 2006 21:28:46 +0200
Local: Mon, Sep 11 2006 3:28 pm
Subject: Re: [Rails-spinoffs] Re: Prototype: "element-id".$() instead of $('element-id')

Ahh. Never knew about .constructor field. Thanks. Nic.

On 9/11/06, Christophe Porteneuve <t...@tddsworld.com> wrote:

--
Dr Nic Williams
http://www.drnicwilliams.com - Ruby/Rails blog
skype: nicwilliams
(m) +31 62 494 8552
(p) +61 7 3102 3237 (finds me anywhere in the world)
(f) +61 7 3305 7572 (sends fax to my email)

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christophe Porteneuve  
View profile  
 More options Sep 11 2006, 3:50 pm
From: Christophe Porteneuve <t...@tddsworld.com>
Date: Mon, 11 Sep 2006 21:50:11 +0200
Local: Mon, Sep 11 2006 3:50 pm
Subject: Re: [Rails-spinoffs] Re: Prototype: "element-id".$() instead of $('element-id')
Nic Williams a écrit :

> Ahh. Never knew about .constructor field. Thanks. Nic.

You're welcome :-)

As for .constructor:

- Used in Array.flatten and Form.Element.serialize.
- Specified in ECMA-262 §15.1.4
- Specified in DevMo:

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Gl...
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Ob...

BETTER YET:

Another way to get this result would be to use the instanceof operator,
which is admittedly more readable (shame on me!):

if (element instanceof String)

- Used in Object.inspect
- Specified in ECMA-262 §11.8.6
- Specified in DevMo:

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Op...

I'll offer a patch to Prototype around this.

--
Christophe Porteneuve a.k.a. TDD
"[They] did not know it was impossible, so they did it." --Mark Twain
Email: t...@tddsworld.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nic Williams  
View profile  
 More options Sep 11 2006, 3:56 pm
From: "Nic Williams" <drnicwilli...@gmail.com>
Date: Mon, 11 Sep 2006 21:56:18 +0200
Local: Mon, Sep 11 2006 3:56 pm
Subject: Re: [Rails-spinoffs] Re: Prototype: "element-id".$() instead of $('element-id')

Except:

>>> "element-id".constructorfunction String() {...}

but:
>>> "element-id" instanceof String

false
(
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Op...
)

So would it still be useful for a revised $() function?

On 9/11/06, Christophe Porteneuve <t...@tddsworld.com> wrote:

--
Dr Nic Williams
http://www.drnicwilliams.com - Ruby/Rails blog
skype: nicwilliams
(m) +31 62 494 8552
(p) +61 7 3102 3237 (finds me anywhere in the world)
(f) +61 7 3305 7572 (sends fax to my email)

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dr Nic  
View profile  
 More options Sep 11 2006, 4:24 pm
From: "Dr Nic" <drnicwilli...@gmail.com>
Date: Mon, 11 Sep 2006 20:24:07 -0000
Local: Mon, Sep 11 2006 4:24 pm
Subject: Re: Prototype: "element-id".$() instead of $('element-id')
How about $H for objects:

Object.prototype.$H = function() {
  return $H(this);

}

This is great for console debugging:

>>> anObject.$H().inspect()

"#<Hash:{'key1': 'value1'}>"

So much fun in 3 lines of code! :)

Nic


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin Bialasinski  
View profile  
 More options Sep 11 2006, 4:48 pm
From: "Martin Bialasinski" <klingel...@gmail.com>
Date: Mon, 11 Sep 2006 22:48:48 +0200
Local: Mon, Sep 11 2006 4:48 pm
Subject: Re: [Rails-spinoffs] Re: Prototype: "element-id".$() instead of $('element-id')
On 9/11/06, Nic Williams <drnicwilli...@gmail.com> wrote:

> So would it still be useful for a revised $() function?

There is no need to change $().

What you see is the legacy of Netscape taking a stride away from the
ECMAScript standard.

Netscape's JS engine treated (and still treats) strings as array of
strings to allow things like "test"[0] to return "t".

I do not know the internal working, but from observation:

When using "this" in a function defined in String.prototype, "this"
behaves like a string, when the context of the use requires a string.
And it looks like the engine knows document.getElementById() requires
a string. If the context does not require a string, like when using
"this" as a function argument to a user defined function, "this"
evaluates to an array of chars.

Therefore, the error is not with $(). Besides the before mentioned
$(String(this)), $(this.valueOf()) will work as well.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin Bialasinski  
View profile  
 More options Sep 11 2006, 4:56 pm
From: "Martin Bialasinski" <klingel...@gmail.com>
Date: Mon, 11 Sep 2006 22:56:29 +0200
Local: Mon, Sep 11 2006 4:56 pm
Subject: Re: [Rails-spinoffs] Re: Prototype: "element-id".$() instead of $('element-id')
On 9/11/06, Dr Nic <drnicwilli...@gmail.com> wrote:

> Object.prototype.$H = function() {

Vade retro, Satanas!

This breaks the "Object is an empty container and can be safely looped
with for( in )" convention.

http://erik.eae.net/archives/2005/06/06/22.13.54


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Kaspick  
View profile  
 More options Sep 11 2006, 4:57 pm
From: "Andrew Kaspick" <akasp...@gmail.com>
Date: Mon, 11 Sep 2006 15:57:34 -0500
Local: Mon, Sep 11 2006 4:57 pm
Subject: Re: [Rails-spinoffs] Re: Prototype: "element-id".$() instead of $('element-id')
There has been a previous thread on the overuse of $ type functions.
I can't remember how long ago that was.... 3-4 months??  Sorry, I
don't have any more information about that though.

On 9/11/06, Dr Nic <drnicwilli...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nic Williams  
View profile  
 More options Sep 11 2006, 5:00 pm
From: "Nic Williams" <drnicwilli...@gmail.com>
Date: Mon, 11 Sep 2006 23:00:38 +0200
Local: Mon, Sep 11 2006 5:00 pm
Subject: Re: [Rails-spinoffs] Re: Prototype: "element-id".$() instead of $('element-id')

That's a pity. I've had lovely fun with it in Firebug for the last half
hour. :(

On 9/11/06, Martin Bialasinski <klingel...@gmail.com> wrote:

> On 9/11/06, Dr Nic <drnicwilli...@gmail.com> wrote:

> > Object.prototype.$H = function() {

> Vade retro, Satanas!

> This breaks the "Object is an empty container and can be safely looped
> with for( in )" convention.

> http://erik.eae.net/archives/2005/06/06/22.13.54

--
Dr Nic Williams
http://www.drnicwilliams.com - Ruby/Rails blog
skype: nicwilliams
(m) +31 62 494 8552
(p) +61 7 3102 3237 (finds me anywhere in the world)
(f) +61 7 3305 7572 (sends fax to my email)

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christophe Porteneuve  
View profile  
 More options Sep 11 2006, 5:59 pm
From: Christophe Porteneuve <t...@tddsworld.com>
Date: Mon, 11 Sep 2006 23:59:47 +0200
Local: Mon, Sep 11 2006 5:59 pm
Subject: Re: [Rails-spinoffs] Re: Prototype: "element-id".$() instead of $('element-id')
Nic Williams a écrit :

> Except:

>>>> "element-id".constructor
> function String() {...}

> but:
>>>> "element-id" instanceof String
> false

Yeah, saw that.  Kinda weird, btw.  So we would have to use BOTH, with a ||.

--
Christophe Porteneuve a.k.a. TDD
"[They] did not know it was impossible, so they did it." --Mark Twain
Email: t...@tddsworld.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christophe Porteneuve  
View profile  
 More options Sep 12 2006, 2:24 am
From: Christophe Porteneuve <t...@tddsworld.com>
Date: Tue, 12 Sep 2006 08:24:50 +0200
Local: Tues, Sep 12 2006 2:24 am
Subject: Re: [Rails-spinoffs] Re: Prototype: "element-id".$() instead of $('element-id')
Martin Bialasinski a écrit :

>> Object.prototype.$H = function() {

> Vade retro, Satanas!

> This breaks the "Object is an empty container and can be safely looped
> with for( in )" convention.

Aside from the '$ overuse' issue, for/in indeed doesn't work too good on
Prototype-extended objects, what with all the methods.  Which is
precisely why we now have Object.keys().  You would replace for/in with
Object.keys().each, I guess.

--
Christophe Porteneuve a.k.a. TDD
"[They] did not know it was impossible, so they did it." --Mark Twain
Email: t...@tddsworld.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin Bialasinski  
View profile  
 More options Sep 12 2006, 4:31 am
From: "Martin Bialasinski" <klingel...@gmail.com>
Date: Tue, 12 Sep 2006 10:31:52 +0200
Local: Tues, Sep 12 2006 4:31 am
Subject: Re: [Rails-spinoffs] Re: Prototype: "element-id".$() instead of $('element-id')
On 9/12/06, Christophe Porteneuve <t...@tddsworld.com> wrote:

> Aside from the '$ overuse' issue, for/in indeed doesn't work too good on
> Prototype-extended objects, what with all the methods.

for ( in ) loops are supposed to work with Objects and they do
flawlessly. This is why Object.prototype is not tempered with. for (
in ) loops do not make sense for the other native data types, and user
defined classes can't mark properties non-enumerable anyway (a
ECMAScript omission), so for ( in ) can only be used for introspection
on them.

Object.keys() is not there because for ( in ) does not work (it does),
it is a syntactic sugar. If you look at the implementation, it is just
a for ( in ) loop. If it would not work properly, Object.keys() could
not have used it, obviously.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google