Aller au contenu | Aller au menu | Aller à la recherche


javascript variable name dollar sign?

When and why use dollar sign or underscore in a variable name ?

3r d Edition - December 1999 - Standard ECMA-262 ECMAScript Language Specification

Identifiers : This standard specifies one departure from the grammar given in the Unicode standard: The dollar sign ($) and the underscore (_) are permitted anywhere in an identifier. The dollar sign is intended for use only in mechanically generated code.

5th Edition / December 2009 - Standard ECMA-262 ECMAScript Language Specification

Identifier Names and Identifiers : This standard specifies specific character additions: The dollar sign ($) and the underscore (_) are permitted anywhere in an IdentifierName.

6th Edition / June 2015 - ECMAScript® 2015 Language Specification

Names and Keywords

NOTE 1 This standard specifies specific code point additions: U+0024 (DOLLAR SIGN) and U+005F (LOW LINE) are permitted anywhere in an IdentifierName, and the code points U+200C (ZERO WIDTH NON-JOINER) and U+200D (ZERO WIDTH JOINER) are permitted anywhere after the first code point of an IdentifierName.

JavaScript Dollar Sign ($) – What Is It For?

Why would a JavaScript variable start with a dollar sign?

_ at the beginning of a variable name is often used to indicate a private variable

Usage to know is a jQuery object :

var $email = $("#email"); // refers to the jQuery object representation of the dom object
var email_field = $("#email").get(0); // refers to the dom object itself

Angular Prefixes $ and $$: To prevent accidental name collisions with your code, Angular prefixes names of public objects with $ and names of private objects with $$. Please do not use the $ or $$ prefix in your code.

Why use $ in the name of javascript variables?

It is usally used to signify a variable holding a jquery or other javascript framework object, because they can have shorthand $ function..

It is just easier to identify the type of the contents.

The $ in the variable name is only part of the name, but the convention is to use it to start variable names when the variable represents a jQuery object.

Naming things

  • Names that begin with _ are usually "private" (more on this later).
  • Names that begin with uppercase letters are usually "constructors," used to create new instances of objects (more on this later as well).
  • In code that uses jQuery, names that begin with $ usually refer to jQuery objects.

valid names in JavaScript are all, but can't begin with a number and don't include a hyphen (-) or a dot (.)

var $myDiv = $('#my_html_id_root');
console.log($myDiv); // Object { length: 1, context: HTMLDocument ? _display, selector: "#my_html_id_root", 1 more… }
$myDiv.fadeOut(); // OK - Getting from the DOM-variable to the jQuery variable

var myDiv = document.getElementById('my_html_id_root');
console.log(myDiv); // <div id="my_html_id_root" style="color: blue;">
// myDiv.fadeOut(); // TypeError: myDiv.fadeOut is not a function
$(myDiv).fadeOut(); // OK - Getting from the DOM-variable to the jQuery variable

var myDiv = $myDiv.get(0); // OK Getting from the jQuery variable to the DOM-variable.

jQuery Naming Conventions: Don't Prefix Variables With $ Using $Variable In jQuery Code Is Just Hungarian Notation

With jQuery often used for :

.click(function(){
    var $this = $( this );
});

Ajouter un commentaire

Le code HTML est affiché comme du texte et les adresses web sont automatiquement transformées.

Fil des commentaires de ce billet