开发者

Reading a javascript script, why so many $ (dollar signs)?

开发者 https://www.devze.com 2023-03-23 02:10 出处:网络
I am trying to decipher a .js script and I am finding it filled with $ throughou开发者_如何学Ct?Is there any reason to use this?I\'m pretty new to JavaScript.I think you are reading a JavaScript libra

I am trying to decipher a .js script and I am finding it filled with $ throughou开发者_如何学Ct? Is there any reason to use this? I'm pretty new to JavaScript.


I think you are reading a JavaScript library famously known as jQuery (or possibly another library). The $ is just a short form for a namespace or use as an identifier.

You can think of it like this jQuery('p') to select all the paragraphs on a page, or for short form you can just write $('p').

Here is a link for jQuery tutorials/docs jQuery

Here is a list of standards section 7.6 describes it in detail ECMA Standard


A number of libraries have used $ as their primary symbol. It's nothing to do with JavaScript per se, but it's a short distinctive symbol and so libraries have tended to glom onto it. (You can start an identifier with $ in JavaScript, and identifiers can be one character long, so $ is a valid identifier, just like a or x.)

I know of at least three libraries that use $ for something:

  • jQuery - It's the all purpose function for jQuery, an alias of the jQuery function; more here.
  • Prototype - It's Prototype's replacement for document.getElementById, more here. Prototype also has $$, which is for looking things up via CSS selectors.
  • MooTools - Same use as Prototype (because MooTools is either "inspired by" or "forked from" Prototype, some years back, depending on who you ask), more here. And like Prototype, it has $$.


$ is a variable. A number of frameworks use it as a short hand for using it. Prototype and Jquery are the two big ones. This does not mean that the $ automatically is either one of those. It could be anything as anything in JavaScript can assign something to the $. This is something to be aware of, because when you start combining scripts from different sources, it's really easy for one to accidentally reassign a variable to something else.

Most likely it is a framework reference, but you'll have to read the code to be sure. At one point in time the $ was meant to be used to indicate that the code referenced by it was auto generated, but this is just a guideline.


if you're new to javascript, $() can look strange.

Try to think of it like

var $ = function(){
    //do something
} 

So the dollar sign is just the name of a variable like any other.

var myFunction = function(){
    //do something
}
// this is exactly the same just a different name. 

So the dollar sign has no special meaning in javascript.

Frameworks like to use it because you are using their functions so often, having to write e.g. jQuery() every time would be tedious. Having one character is nice and short.

I think they also have a preference for the $ symbol, purely because it is unusual so it is quickly distinguishable from other code.

A quick way to find out if it is jQuery is to do console.log(jQuery) if the console returns a string of code $() is jQuery. If you get undefined, it is something else.


Maybe you're reading jQuery code.

Because JavaScript lets you define variables which start with $ sign, or literally which are only $. For example, you can do:

var $ = "something";
alert($);

jQuery is a library built on JavaScript (the most popular at the time) and it has a global object to keep everything encapsulated. You access that global object using $.


The script is probably using a third party library such as Prototypejs or jQuery or he defined his own function $() which explain why the dollar sign appears so often in that script.


It's a jQuery function. That's what it is most probably. Might also be the Prototype library, or just a function that does something that's needed many times in the code, like getElementById etc


Without seeing the code, it sounds like the script you are looking at makes use of jQuery, as the $ is the default jQuery object.


Typically the $ will represent jQuery or another specific library (Moo Tools, etc.) . $ is the shortened form of referencing the jQuery object (or whatever library it was assigned). It makes the code much more readable and easy to use.

If you are just learning javascript, you will soon become very familiar with jQuery. :)


Either its jQuery or an old PHP habit by some javascript programmer :)


$ is only a function. It means you work with some javascript superstructure (framework).

0

精彩评论

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