admin管理员组

文章数量:1356270

This function declaration doesn't make sense to me. What does the $ sign do?

function $(id) { return document.getElementById(id); }  

Also, this code is interfering with other Javascript code. If I ment it out, then the other Javascript code I'm trying to use works, but then I lose some other functionality.

Why might it be interfering with my other code.

This function declaration doesn't make sense to me. What does the $ sign do?

function $(id) { return document.getElementById(id); }  

Also, this code is interfering with other Javascript code. If I ment it out, then the other Javascript code I'm trying to use works, but then I lose some other functionality.

Why might it be interfering with my other code.

Share Improve this question asked Jan 26, 2012 at 23:19 node ninjanode ninja 33.1k61 gold badges173 silver badges257 bronze badges 2
  • 6 Some JavaScript ninja you are :/ – j08691 Commented Jan 26, 2012 at 23:23
  • What is the meaning of “$” sign in javascript – user1106925 Commented Jan 26, 2012 at 23:28
Add a ment  | 

4 Answers 4

Reset to default 4

What does the $ sign do?

Nothing special. It is just a character that can be used in variable/function names.

There has been a trend to use $ as the name to the gateway function for a number of libraries (including Mootools, Prototype and jQuery). This has two major problems:

  • It makes them conflict with each other
  • It violates the principles of self-documenting code

Also, this code is interfering with other Javascript code.

Presumably something else is using a variable called $ then.

You are declaring a function called $ that wraps document.getElementById, apparently for convenience. Nothing special.

Reference What characters are valid for JavaScript variable names?


As for the interference, there is likely another global variable called $, which is being clobbered. See jQuery.

JavaScript variables can start (and only consist of) a $ (amongst others).

Previously, the $ was added solely for machine generated variables.

This was in the spec I believe, but has since been removed (don't quote me).

Dollar sign was added to the language specifically for use by code generators and macro processes, so if you have machines writing code then the machines need to be confident that the variables that they create will not conflict with variables that the humans are going to create. To distinguish them, we’ll allow the machines to use dollar sign. Some of the ninjas found out about that and thought oh, dollar sign, I can use dollar sign as a function name, so they’re out there doing that. And it looks stupid. I mean, look at a program with dollar sign.

Source: Douglas Crockford.

If it is interfering with other code, you may be using a library which uses this identifier. If that's the case, look at the no conflict mode for that library (if it has one).

Let me guess: are you using jQuery or prototype?

$ and _ are valid characters in variable/function names. The code is the same as declaring function a(id) {return document.getElementById(id);}.

It's typically used as a shortcut for selecting an element because typing document.getElementById is much too long for how often it's used.

本文标签: Javascript function declarations with a dollar signStack Overflow