Home jquery What is the difference between variables VAR A and VAR $ A...

What is the difference between variables VAR A and VAR $ A in JavaScript?

Author

Date

Category

Good day!

more and more often in JS scripts come across the proven variable using the dollar sign, i.e. Instead of a simple ad var a I see Var $ a , and in the following code of appeal to this variable, then so $ a , then so $ (a) . Here, for example, a piece of script:

var forms = function () {
  $ ('. JS_FORM'). Each (Function () {
    VAR $ Form = $ (this),
      $ CustomField = $ Form.find ('[Data-Error]'),
      CustomFieldRulls = {};
    var zindexs = function () {
      SetTimeout (Function () {
        $ ('label.error'). Each (Function (I) {
          $ (this) .css ({'z-index': '50'-i})
        });
      }, 200)
    };

Used both ordinary variables and “variables with a dollar”. I do not understand what is the difference between them? It is just done for convenience, or is it something else from a functional point of view?


Answer 1, Authority 100%

I can not be sure of all 146%, but there is a feeling that the dollar icon here is noted the fact that the value of the variable is obtained using a jQuery selector. While other, ordinary JS variables go without this prefix. In principle, it looks sufficiently logical – JS is a language with dynamic typing, and understand by the name of the variable, which is stored in it, it is not always possible. Therefore, the prefix in the form of a dollar facilitates this understanding.

However, regardless of whether my assumption is true, or not, some new feature announcement “with a dollar” does not carry – in JS it is the same symbol that can be used in identifier names, which is other


Answer 2

not quite so. Based on the latest JQuery specification:

& lt; div id = "selector" & gt; & lt; / div & gt;
FUNCTION READY () {
 var selector = $ ('# selector');
 var selector = $ ('# selector'). context;
 var $ selector = $ ('# selector');
}
Window.Onload = Ready;

In the first case, Selector will return [Object Object]

In the second [Object HTMLDocument] (so, for completeness of the picture)

And in the third [object HTMLDIVEEMENT]

Briefly, the third option will work as document.getelementbyid (‘selector’); on pure JS.

And there are already looking there yourself, to whom with what and how to work.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions