开发者

What is the purpose of the $ operator in a javascript function declaration? [duplicate]

开发者 https://www.devze.com 2023-04-02 01:00 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Why would a javascript variable start with a dollar开发者_开发技巧 sign?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Why would a javascript variable start with a dollar开发者_开发技巧 sign?

Can someone explain the dollar sign in Javascript?

I was looking at a js tutorial where several of the functions where declared with a $ in front of them. I have done some on-line searching but can find no reference to this. Here is an example I created myself:

var $x = function(q)
{
  return q*q;
}

var $y = function()
{
  alert($x(10));
}

When I call $y from a webpage I get an alert with 100, which is what I expect. So, what, if anything, is the $ for?


I don't think $ means anything special, the tutorial just decided to name the function $x and $y


It doesn't mean anything special unless you've included jQuery. Generally, I've seen $ used in the beginning of variables to mark them as global variables.


$ is one of the allowed signs for names of variables in JavaScript. It is not an operator.

As some other answer on SO mentioned, allowed characters are: [a-zA-Z_$][0-9a-zA-Z_$]*.

So, this is not an operator, and can be used to distinguish some variables from other variables.

When it comes to single dolar sign ($, without any other letter), this is common as alias for jQuery function, if you have installed jQuery and stick with default settings.


In this particular case, serves no purpose. You can remove it an simply do y(10); or x(10);

For example Microsoft introduced $find as a shorthand for document.getElementById

0

精彩评论

暂无评论...
验证码 换一张
取 消