programming languages - why javascript permits $ for name of functions? - Stack Overflow

I am starting to love jQuery but as PHP developer I hate using that $().stuffas everyone know $ in PHP

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

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

Share Improve this question edited Mar 19, 2011 at 13:13 asked Mar 19, 2011 at 13:09 anonanon 6
  • how can i make this question CW – anon Commented Mar 19, 2011 at 13:15
  • Nothing like checking the spec: ecma-international/publications/standards/Ecma-262.htm (Section 7.6). Identifiers can consist of one or more of any unicode character, specifically including $ and _. – T.J. Crowder Commented Mar 19, 2011 at 13:16
  • @yes123: "how can i make this question CW" If you click edit, there may be a "Community Wiki" checkbox under the question box. If there is, tick it and click Save Edits. If not, you don't have enough rep yet to make it a CW after the fact (you could when originally asking the question). – T.J. Crowder Commented Mar 19, 2011 at 13:18
  • @T. J. Crowder it's not really any unicode character; there's (apparently) a Unicode "identifier" concept and that's what Javascript follows, so identifiers can have all sorts of crazy things but not spaces or mas etc ... – Pointy Commented Mar 19, 2011 at 13:21
  • @crow: ok i will stick with normal question – anon Commented Mar 19, 2011 at 13:21
 |  Show 1 more ment

5 Answers 5

Reset to default 7

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 puter 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.

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.

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.

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.

You can do

var whatever = $.noConflict();

now you can use whatever(..) instead of $(...)

documentation at http://api.jquery./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).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744311336a4567962.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信