$ at the beginning and at the end of variable
Possible Duplicate:
Why would a JavaScript variable start with a dollar sign?
I realized some syntax with jquery and didnt know what it is. here goes:
var $foo = $("bar").blah();
var hello$ = $("stgelse").etc();
What is the difference between these? and why are they like that?
Also, For example:
var $foo = $("").blah();
var should already contain whatever right side is returning ? no?
Can you please elaborate?
var $foo = $("bar").blah();
var
is a keyword that makes the variable local in certain cases. See this question $foo
is the name of this variable. There is no need for the $
, and indeed it confuses everything, but some languages (like PHP) need them to say "this is a variable". It could've been just foo
=
is the assignment to make $foo
have whatever comes from the right hand side $
is another variable, but a special one: this is your JQuery thingy! From there you see it does all the JQuery stuff, like "find bar
, do blah()
etc. The same basically goes for the other line, but in this case someone thought that hello$
would be a nice name for a var :)
In Javascript you can use $
in a variable name (or as a variable name all by itself, like it's used by the jQuery library for example), there is no special meaning in the language itself.
Some people use $
at the beginning of a variable name to indicate that the variable should contain a jQuery object, for example:
var $form = $('form');
A $
at the end of variable names was used in BASIC for string variables, which might be one reason for using it to mean something when naming variables in other languages also. Example:
10 LET NAME$="JOHN DOE"
20 PRINT NAME$
$
is just a name - names in JavaScript can contain dollar signs, and can consist of just a dollar sign.
Its like just normal javascript variable but as per convention is used to identify jquery objects variable from normal javascript variable. there nothing special about it.
链接地址: http://www.djcxy.com/p/94954.html下一篇: $在变量的开头和结尾