This question already has answers here:
Why would a JavaScript variable start with a dollar sign? [duplicate]
(16 answers)
Closed 9 years ago.
So I recently discovered that it is useful to name jQuery variables in a way that starts with "$" sign, so var $progress instead of var progress for example.
What I don't understand is, how do you know when to use this? I mean there is such a thin line between javascript and jQuery (there isn't even a line, jQuery is JavaScript) would I use it on variables that are changed with jQuery or something?
At this moment it seems to me that if you have a jQuery based solution that all variables would end up starting with "$", so I'd like to know when to actually do this so I don't confuse myself and others.
There is no need to use $ at the beginning of variable names and some developers actually hate the practice altogether, though I do personally like it. The common use is to note that the value of that variable is a jQuery object.
A jQuery object is what is created in this process:
var $myElement = $('#some-id');
That notes that $myElement contains a jQuery object and therefore it is safe to use jQuery functions on it. For example, $myElement.hide();
Without jQuery, this is how the same code might look:
var myElement = document.getElementById('some-id');
myElement.style.display = 'none';
In that case, myElement is NOT a jQuery object and so myElement.hide() is an error. This is an example of why someone may prefer to have the $ on the variable name... to note that jQuery functions can be called on it. If someone is using jQuery already, it is standard to get all element references with jQuery anyway, so this actually is redundant.
This is really all about coding standards that you and your team have to decide on. It's the same as debating underscores or pascal casing or any other naming convention that you can come up with.
In short: It comes down to personal preference.
Related
This question already has answers here:
How do I get query string value from script path?
(5 answers)
Closed 8 years ago.
I wanna know is this possible I pass get parameters for a javascript file and use them in codes?
I have this in html:
<script type="text/javascript" src="/javafile.js?q=somtext"></script>
know how I use this "q" parameter in my script codes? is this possible?
You can either:
Generate the JS dynamically with a server side language and read the query string there
Assume that the JS is the last <script> element so far in the DOM and parse document.scripts[document.scripts.length - 1].src
Note that if the script element has been added dynamically (i.e. with JS), it might not be the last script in the DOM.
I think Quentin's suggestion is the answer to your question.
I usually use an alternative way for this, which may also help you:
Make sure that your javascript is written in a library form, and make sure that you have an instantiate method/function inside your javascript that allows you to pass parameters (or better, as an object)
// on dom load:
library.init({ var1: value1, var2: value2});
This allows you also to load your javascript in a different way, and allows for cleaner code.
Or you can use option 3: use a library that has this functionality, e.g. MooTools: http://mootools.net/docs/more/Types/String.QueryString
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
what is the meaning of "$" sign in javascript
If Jquery is just external javascript code, then how does it use the dollar sign? Wouldn't that be adding new syntax?
$ is just a normal variable. You can do var $ = 42;.
jQuery - and any other javascript framework, simply encapsulate existing functionality in different utility functions. Most platforms endeavor to cover different browser implementations.
So, jQuery's dollar-sign operator is simply a function in which javascript functionality is encapsulated. It is, in essence, an alias for document.getElementById, but it also covers getting by class name or tag name.
As for adding new syntax, in short: no, it does not. The look and organization of code written in a framework may be different, but at the core you're still writing javascript, you're just using a set of functions provided by the library instead of using the set of functions in javascript core.
var $ = function () {
// do something here
}
I am trying to decipher a .js script and I am finding it filled with $ throughout? 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).
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the difference between $ and jQuery
I've noticed cases where "jQuery(selector)" is used instead of "$(selector)", what is the difference between both of any?
None. Normally, the $ variable simply points to jQuery. You might be using jQuery instead in cases where the $ is used by another library, or you are using it for other purposes in your code.
None. $ is merely an alias to jQuery. However, when developing code for public consumption, you should use jQuery instead of $, as $ might be assigned to something else, if they use more than one framework. Or just use a closure to make $ a private variable, which is what most plugins do:
(function($){
// Use $ normally here...
})(jQuery);
Its just an alias for the same thing - to avoid conflicts in something like Wordpress, or frameworks that might import their own version of jQuery.
jQuery is used when you have another javascript library that uses the $ variable. jQuery===$
I was having issues with some jquery and posted about it here. After following a few of the suggestions, I was able to isolate the problem - IE8 did not like the variable name new_email. In fact the debugger had been telling me that the issue was at character 4 of that line, but I couldn't believe it was the variable name, so I kept looking for other issues.
After finally giving in and changing the variable name to newEmail, IE8 no longer blows up - the code works as expected with no errors.
I've been unable to find any documentation stating that you can't use underscores in jquery variable names, and indeed, the code worked correctly in every other browser with the underscore in place. Is this an unwritten rule in IE8? Is it something that real jquery developers just know? I'm worried if this really is true, as I inherited this code, and the app is enormous - I know there are several dozen variables in various places that have underscores in them.
This is actually a javascript variable and not a jQuery variable, an important distinction, and in Javascript the underscore is a valid character for variable names. You must have changed something else unrelated.
Is it possible that variable name was already assigned elsewhere? Also note that you aren't using the var keyword which can cause further issues with scope.
You can always post a jsfiddle.net example if you would like more assistance though.
here is a working jsfiddle that uses your variable
please note that you should probably be more specific than ":text"
jQuery is written in JavaScript, which is a language based on the ECMAScript Language Specification (PDF). The specification states that "underscore[s] are permitted anywhere in [a variable name]".
Your problem, as HurnsMobile, stated is most definitely not with the underscore but some other part of your code. It may also be caused by some quirk or bug in IE8 but even IE8 should be able to handle simple variable names.