JQuery: What is the difference between "var test" and "var $ test"

What is the difference between these statements? I know that "var $ test" declares a jquery variable, but what is the difference between a jquery variable and a common javascript variable?

+15
javascript jquery
May 21 '10 at 9:28 a.m.
source share
4 answers

Nothing. No difference. $ is a valid character in the JavaScript identifier, but does not have much meaning. In fact, the ECMAScript 3 specification in section 7.6 states that

The dollar sign ($) and underscore (_) are allowed anywhere in the identifier. The dollar sign is intended to be used only by mechanically generated code.

... although events now probably made this recommendation obsolete. Indeed, the recent ECMAScript 5 specification excludes the last sentence.

+24
May 21 '10 at 9:29 a.m.
source share

$test is the convention used to indicate that this variable is a jQuery object, and you can call standard functions on it, and test can be a standard DOM element, and you cannot call the val function on it for an example.

For example:

 var $test = $('#someId'); var test = document.getElementById('#someId'); 

You can do $test.text(); but you cannot do test.text();

+29
May 21 '10 at 9:30 a.m.
source share

there is no difference between var $test and var test . but it’s always useful to know what data is in your variable: (for example, if someone just wants to change the function in your code without having to read or use console.log to find out what is in it) / p>

Here are some examples:

 var arrTest = [1, 2, a, b, c], //Array objTest = {test: 1, test2: 2}, //Object strTest = "test", //String intTest = 4, //integer $test = $("#test") //jqueryObject 

but it will also work as follows

 var Test1 = [1, 2, a, b, c], //Array test2 = {test: 1, test2: 2}, //Object Test3 = "test", //String Test4 = 4, //integer $test = $("#test") //jquery Object 

I think the jQuery $() form confuses you.

Jquery is basically a function set in the variable name $ :

 var $ = function(e){ // the jquery magic } 

but you can use $somethingelse as a variable.

+2
May 21 '10 at 9:40 a.m.
source share

Try to criticize jQuery.

The differences between inline javascript and a framework like jQuery is zip - it exists.

If you want to learn javascript, skip jQuery.

I use GMap because of its functions - I cannot independently develop the functionality of GMap.

But jQuery and extJS just offer a framework - there is no funtionallity that you cannot do yourself.

Mike

-one
May 21 '10 at 10:10
source share



All Articles