Is there a need for the $?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Is there a need for the $?

Post by JellyFish »

Most javascript libraries use the $ function to select elements. It never really impressed me; I never liked having to hold shift and pressed a number before, it's just so out-of-the-way. Soo, I was thinking lately, if I created a javascript toolkit I wouldn't use a funciton like this but instead:

Code: Select all

 
Object.prototype.foo = function(){
alert(this);
}
 
"Hello".foo(); // would alert "Hello"
(23).foo(); // would alert 23
(document.body).foo(); // would alert [object HTMLBodyElement]
(function(){}).foo(); //would alert function(){}
 
This way I wouldn't need to type $. :D And I'd be happy. Also I wouldn't really need to use () in sum cases, such as "(document.body).foo()" could be "document.body.foo()". Brilliant IMO!

I feel like the $ is so cryptic, so to speak. Without the $ it would feel more like javascript instead of a very, very punctual language.

What would this new idea be lacking, in feature, ability, etc?

Thanks for reading. Cheers.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Is there a need for the $?

Post by onion2k »

$() is actually a Javascript function called $ that goes away and fetches the element named in the brackets. You can't attach a function to a string like "Hello", so no, what you want is not possible ... the $ is necessary. You could call it something else of course.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Is there a need for the $?

Post by superdezign »

The reason that the "$" character is used is because it is a special character, but is allowed to be used freely in most programming languages as any other character. I've made a small JavaScript framework myself, and I use the letter "V" (for Volectricity, of course :P).
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Is there a need for the $?

Post by JellyFish »

onion2k wrote:$() is actually a Javascript function called $ that goes away and fetches the element named in the brackets. You can't attach a function to a string like "Hello", so no, what you want is not possible ... the $ is necessary. You could call it something else of course.
Oh, never knew that the $ function was a built-in javascript function. But you can attach a function to a string:

Code: Select all

 
String.prototype.foo = function() {
//function code
}
 
"Hello".foo();
 
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Is there a need for the $?

Post by Kieran Huggins »

JellyFish wrote:Oh, never knew that the $ function was a built-in javascript function. But you can attach a function to a string:
It's not a native function, but it's been used by several popular frameworks.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Is there a need for the $?

Post by Zoxive »

You can use what ever you want really..

Code: Select all

function _(Obj){
  return $(Obj);
}
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Is there a need for the $?

Post by JellyFish »

Zoxive wrote:You can use what ever you want really..

Code: Select all

function _(Obj){
  return $(Obj);
}
The point isn't to have a different symbol other then $. Rather, the point is not to have a symbol at all. Reason: make the framework feel more like it's apart of the language.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Is there a need for the $?

Post by Christopher »

I think the point they were making is that you need a function name to have a function. $ is a valid function name in javascript, but a function name can't start with ( or ".
(#10850)
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Is there a need for the $?

Post by JellyFish »

arborint wrote:I think the point they were making is that you need a function name to have a function. $ is a valid function name in javascript, but a function name can't start with ( or ".
It's not necessarily a function without the $. But functionality, that remains.

Code: Select all

 
//jQuery
$("body img").css("backgroundImage", "url(http://google.com/images/logo.gif)");
 
//My oddity
"body img".css("backgroundImage", "url(http://google.com/images/logo.gif)");
 
This is doable.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Is there a need for the $?

Post by Christopher »

So you are going to extend the String object to add jQuery like functionality?
(#10850)
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Is there a need for the $?

Post by JellyFish »

arborint wrote:So you are going to extend the String object to add jQuery like functionality?
Exactly. I'd like to know everyone's opinion on this.
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Re: Is there a need for the $?

Post by arjan.top »

its confusing, string object is for string manipulation (is this the right word? :D )
Post Reply