Reputation: 8694
I don't grok the idea/purpose/use of Javascript $, as in
function $(id) { return document.getElementById(id); }
Could someone please explain or point me at an explanation?
Thanks!
-- Pete
Upvotes: 2
Views: 761
Reputation: 346260
There are two main reasons to use $
as a function name, especially in frameworks like jQuery:
Upvotes: 2
Reputation: 99957
It's just the name of a function called $()
. The dollar sign ($) is a valid character for identifiers in JavaScript. jQuery, for example, uses it as a shorthand alias for the jQuery()
function.
Upvotes: 2
Reputation: 413712
When you see JavaScript code that involves lots of $(foo)
function calls, it's probably using either the jQuery or the Prototype web development frameworks. It's just an identifier; unlike a lot of other languages, identifiers (function and variable names) can include and start with "$".
Upvotes: 8
Reputation: 26766
Most commonly this is used by JQuery - specifically, JQuery creates an object with a reference of $
which has various methods to simplify page manipulation.
It's technically possible for anything to attach a class to $
Upvotes: 2
Reputation: 322462
In your code it is the name of a function.
function $(id) { return document.getElementById(id); }
$("my_id")
function myfunc(id) { return document.getElementById(id); }
myfunc("my_id")
Two functions, two different identifiers.
Upvotes: 7