This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why would a JavaScript variable start with a dollar sign?
I am not sure what $scope and $defer are but they just seem to work.
function xyz($scope, $defer) {
xyz = this;
this.defer = $defer;
this.scope = $scope;
this.scope.test = function(re) {
console.log(this,arguments);
}
}
Generally these days devs name a variable $something to flag that it is an object type of a wrapping framework. For example, to cache a jQuery object, it makes sense to use $this = $(this);
That being said, there's nothing special about the dollar sign. Just a heads up for devs.
A bit of history and reasoning; ECMAScript 3 says:
The dollar sign ($) and the underscore (_) are permitted anywhere in
an identifier. The dollar sign is intended for use only in
mechanically generated code.
Whereas ECMAScript 5 says:
The dollar sign ($) and the underscore (_) are permitted anywhere in
an IdentifierName.
So when someone says "Hey you're not supposed to use a dollar sign in your var cuz it's for MECHANICALLY-GENERATED code!" you can say "Psh, ECMA 5, hello?"
Those are names of parameters.
UPDATE: $ is perfectly valid (even by itself, which is probably more common thanks to certain JS libraries) as an identifier in JavaScript, but it isn't usually used as a sigil like this. I'm guessing the code was written by somebody with too much Perl experience, or possibly somebody who mostly deals with jQuery-based code.
Related
This question already has answers here:
What is the purpose of the dollar sign in JavaScript?
(12 answers)
Closed 5 years ago.
I was watching a video about object literals in JS. Then the guy in the video did something like this:
var Facebook = {
name: 'Facebook',
ceo: {
firstName: "Mark",
favColor: "Blue"
},
$stock: 110
};
My question is why is there a $ sign in front of stock? Is there a special meaning? or did he just use it for naming purpose only? I entered $ in console and got something like this:
function $(selector, [startNode]) { [Command Line API] }
I understand that $ sign is used as a selector for JS libraries like JQuery, but what is it's significance in pure JS?
It's just a character. If you saw something in the console it's because some script loaded by that page assigned it a value and it was still in the global scope.
Sometimes (like with jQuery or Angular) it can get used by convention to denote that the value assigned to that variable or property is related to those libraries somehow.
In your example it's just the name of a property.
The $ has no special meaning in JavaScript. It's just a valid variable name. See this answer.
This question already has answers here:
What does function($) mean in javascript?
(3 answers)
Closed 9 years ago.
I just started learning JavaScript. Found a statement function($) { ...} while checking out examples. Can anyone tell me what function($) means?
It means "This defines a function. When it is called: create a local variable called $ and assign the value of the first argument to it."
First off: This creates a function where $ is the first argument passed to it. It could look like this potentially:
function dollar($){
alert($);
}
dollar("hello")
//alerts "hello"
Typically this is used when you want the $ to mean jQuery.
For example:
(function($){
//stuff where $ === jQuery
})(jQuery)
Means that jQuery will be passed into the $ variable for the scope of anything that happens in that function.
This can be useful if you have multiple libraries within the global scope that may use the $ variable, but you have a modular plugin that refers to the necessary library as that and you don't want to rewrite the whole thing.
Note: It does not always mean jQuery but in about 80% of cases it will. Otherwise it is just a convenient way to bind a library to a shorter variable within a certain scope.
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
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
why results varies upon placement of curly braces in javascript code
We have company policies that dictate that in PHP opening curly braces should be on their own lines for readability and so that they can line-up with the closing brace; thus:
if (true)
{
...
}
but in JS they should be kept on the same line, in case there are problems with browsers incorrectly interpretting it.
if (true) {
...
Is the above italic part a legitimate concern?
PS - I suspect that this question has been asked on here already, but I've not found a question that exactly matches mine. Apologies if it's there and I didn't find it.
Yes, it matters in certain corner cases.
And the problem isn't with "browsers incorrectly interpreting it". The dodgy behaviour is correct according to the ECMAScript specifications. A JavaScript implementation that didn't exhibit this behaviour would not be spec-compliant.
An example. This function is broken:
function returnAnObject {
return
{
foo: 'test'
};
}
It's supposed to return an object, but actually returns nothing. JavaScript interprets it like so:
function returnAnObject {
return;
{
foo: 'test'
};
}
The interpretation in JS, is usually when you have line without semi-colon, it is by default added at the end of line. To avoid such things and to increase readability, the braces are usually added on same line as IF, WHILE, Function etc.
This feature in JS is referred to as implicit semicolon insertion as far as I know.
This question already has answers here:
Closed 13 years ago.
What's the difference between var $x and var x in javascript?
Nothing. People tend to use the $x syntax because it's easier to remember you're dealing with a jquery object rather than an element or an id.
In general I tend to use something similar to:
var $x = $(selector) //$x holds reference to a jquery object
var elX = document.getElementById(id); // elX hold ref to an element node
var xId = $(selector).attr('id'); //xId holds ref to an id attribute
The difference? One variable starts with $.
And neither has anything to do with jQuery - it's just javascript.
One declares a variable called $x, one declares a variable called x. Dollar is a perfectly valid character for a variable name in JavaScript (this isn't really specifically jQuery related as far as I can see).
See "Why would a javascript variable start with a dollar sign?" for more.
There is no difference between two in JavaScript. $ is allowed in variable declaration in JavaScript
The dollar prefix is often used in Javascript for global variables. It's merely a convention - Like underscore is often used to denote a private member.