Page 1 of 1

Is there a need for the $?

Posted: Mon Feb 11, 2008 6:31 pm
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.

Re: Is there a need for the $?

Posted: Tue Feb 12, 2008 3:32 am
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.

Re: Is there a need for the $?

Posted: Tue Feb 12, 2008 9:17 am
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).

Re: Is there a need for the $?

Posted: Tue Feb 12, 2008 2:22 pm
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();
 

Re: Is there a need for the $?

Posted: Tue Feb 12, 2008 2:51 pm
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.

Re: Is there a need for the $?

Posted: Tue Feb 12, 2008 3:49 pm
by Zoxive
You can use what ever you want really..

Code: Select all

function _(Obj){
  return $(Obj);
}

Re: Is there a need for the $?

Posted: Tue Feb 12, 2008 4:32 pm
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.

Re: Is there a need for the $?

Posted: Tue Feb 12, 2008 5:00 pm
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 ".

Re: Is there a need for the $?

Posted: Tue Feb 12, 2008 8:06 pm
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.

Re: Is there a need for the $?

Posted: Tue Feb 12, 2008 9:45 pm
by Christopher
So you are going to extend the String object to add jQuery like functionality?

Re: Is there a need for the $?

Posted: Tue Feb 12, 2008 9:52 pm
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.

Re: Is there a need for the $?

Posted: Wed Feb 13, 2008 1:39 pm
by arjan.top
its confusing, string object is for string manipulation (is this the right word? :D )