Reputation:
I am starting to love jQuery but as PHP developer I hate using that
$().stuff
as everyone know $
in PHP marks variables. I guess jQuery in javascript declares a function like this
function $(element) {/*stuff*/}
Does javascripts permits $
as the name of a func? Is that a good behaviour?
I knew only standard [0-9a-zA-Z_]
could be the name of funcs/vars.
Am I missing something?
Thanks
Upvotes: 0
Views: 217
Reputation: 89375
The use of $
in PHP was a flat-out mistake, which was also made by the inventor of Perl. Variable prefixes are redundant in a full-fledged computer language because identifiers like foo
and bar
can be assumed to be variables without qualification — what else would an identifier be identifying? :-) The problem is that the amateurs who invented these languages were very familiar with the Unix shell, and they apparently did not realize that the $
that goes in front of shell variable references like $PATH
is not a beautiful language feature, but an ugly necessity imposed by the fact that, in a shell script, words can appear normally “as themselves”, as when you say:
$ echo PATH
PATH
$ echo $PATH
/usr/local/bin:/usr/bin:/bin
In other words, an escape character like $
that says “this is a variable!” is necessary in a text substitution language like the shell, and escaping is also important in things like TCL and, of course, in modern templating languages, where you have to be able to tell apart all of the normal text that should be copied into the web page apart from the variable references and loops that pull in dynamic information.
But normal programming languages do not work by text substitution, so the $
in Perl and PHP is cruft and noise.
Since $
has no other use in JavaScript, it was a simple aesthetic judgement about whether to make it available as a “normal letter” for variables. JavaScript said “yes”, Python said “no”, and I have gotten used to both choices. I guess it now “looks like JavaScript” to me for $
to be valid!
But in no case should you let your aesthetic judgement here be informed by the horrible choices of Perl and PHP, which were wrong choices! Instead, ask yourself whether $
looks enough like a capital S
that you can consider it to be “close enough to a letter”, or whether it looks forever like a symbol to you. That's the real choice.
Upvotes: 7
Reputation: 195982
You can do
var whatever = $.noConflict();
now you can use whatever(..)
instead of $(...)
documentation at http://api.jquery.com/jQuery.noConflict/
and quoting Values, Variables, and Literals
A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9).
Upvotes: 2
Reputation: 19560
Yes, it's valid. Crockford hates it, others love it. If you don't like it, don't use '$', use 'jQuery'. You can even put jQuery into no conflict mode so that $ never gets set. That said, if you're writing enough JS via PHP that it's causing you problems, you should probably consider learning how to write better, more generic JS.
Upvotes: 4
Reputation: 237847
From the MDC docs:
A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).
I think the key concept here is that, in Javascript, functions are first-class. This means that they are objects, just like everything else. The name you use to refer to a function is effectively a variable.
So, as an example, you can do this:
var myFunc = function() { /* some code */ };
and then call the function as myFunc()
.
Since $
is a valid character in identifier names, it is a valid name for a function just as much as a variable containing anything else.
In my opinion, the use of $
as a sigil for every variable in PHP is bone-headed.
Upvotes: 4
Reputation: 1013
Javascript just allows more then just a-z for function names. $ is a valid name for a function and $ has no special meaning like in php. So $ is a correct name for a function.
Upvotes: 3