What is the $ character used in JavaScript

I am involved in JavaScript and have studied this question, but without success. What does the $ character used in JavaScript mean besides regular expressions? Any resources or readings on this subject would be appreciated. Thank.

+48
javascript jquery
Jun 24 '10 at 6:01
source share
3 answers

This does not mean anything special.

But since $ allowed in identifier names, many Javascript libraries use $ as the "central" interface for them, or at least as a shortcut to access their functions.

For example, if you use jQuery and you say $("div") , this is a call to the $ function with the argument "div". When you say $.post() , it calls the post method on the $ object (Javascript is good in that functions are first-class objects).

+56
Jun 24 '10 at 6:06
source share

I met him in JavaScript when I started using the Prototype platform. In Prototype, $ is simply the name of a commonly used function (very, very simplified - short for document.getElementById ). Personally, I like his patience.

Afaik, it is not used by the language itself.

What it costs, Douglas Crockford advises against using $ in variable / function names you write

Do not use the $ sign (dollar sign) or \ (backslash) in names.




Adding another, rather stubborn quote from Mr. Crockford reads: “ And then there was JavaScript ”:

The dollar sign was added to the language specifically for use by generators and macro processes, so if you have machines that write code, then the machines must be sure that the variables that they create will not conflict with the variables that people are going to create. to distinguish them, it is good to allow machines to use the dollar sign. Some of the ninjas found out about this and thought about, the dollar sign, I can use the dollar sign as the name of the function, so they do it there. And that looks stupid. I mean, look at the dollar sign program.

+24
Jun 24 2018-10-06T00:
source share

If you ask why some variables and function names start with $ , then this is just a convention when using jQuery and / or AngularJS.

In code using jQuery , $ often used as a prefix for variables that contain jQuery selections. for example var $container = $('.container'); .

In AngularJS, they use the $ prefix to mean "Angular core functionality." This way, you know which methods and services are added to the framework and which are common to your application.

+8
Dec 04 '14 at 3:52
source share



All Articles