what does $ mean in pure Javascript [duplicate] - javascript

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.

Related

Javascript square brackets around method name [duplicate]

This question already has answers here:
What do square brackets around a property name in an object literal mean?
(2 answers)
Closed 5 years ago.
In the RxJs doc I found following code snippet:
[rxSubscriberSymbol]() {
return new SubjectSubscriber(this);
}
Its part of the Subject source code and is the first method right after the constructor.
So what do square brackets mean in this context?
those are symbols, which is very similar to defining properties but gives different accessibility and testability functionality and they are completely unique,
you can read a lot more about metaprogramming here,
Metaprogramming in ES6: Symbols and why they're awesome

Use of $ in regular method [duplicate]

This question already has answers here:
Why would a JavaScript variable start with a dollar sign? [duplicate]
(16 answers)
Closed 8 years ago.
I go over some code and I saw the following code
var failedExtPlugins = PluginR.$getFailPlug();
It seems that this time the $ is not refer to jquery object (yes I know that when you use Jquery you start with $) I guess,so what does it mean?
it look like regular method...but way the use $
It looks just like a part of a name. I like to use $ in a name of jQuery objects I have. Maybe author has some thoughts about that.
Its a jQuery wrapped object. $ObjectName reflects that its holding a reference of jQuery wrapped object of particular dom object.
For Example:
var $obj = $("#SomeId");

What does function($) mean? [duplicate]

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.

JavaScript function what is scope and why using $ [duplicate]

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.

What is the purpose of the $ operator in a javascript function declaration? [duplicate]

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

Categories