Use of $ in regular method [duplicate] - javascript

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");

Related

what does $ mean in pure Javascript [duplicate]

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.

Tricking Javascript variable [duplicate]

This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed 7 years ago.
I have a question that I could not find the answer, or perhaps cannot phrase the way it should...
I would like to trick javascript's way of handling variables...
Let's say in php I could do something like:
$test['usr_'.$id]=826
But when I try to do the same in Javascript/jQuery:
$("#usr_rank_h").val('rank_'+id);
It will output rank_826 instead of the value of the var rank_826
The equivalent idiom in javascript is actually
var id = 826;
var test = {};
test['rank_'+id] = 826;
Which gives you back an object of the form
{
'rank_826': 826
}
PS: I'm not sure why you are using jQuery in this case, are you getting the id from an input ?

Why need to pass jQuery object as parameter to closure? [duplicate]

This question already has answers here:
Why define an anonymous function and pass it jQuery as the argument?
(5 answers)
jQuery dollar sign ($) as function argument?
(4 answers)
Closed 9 years ago.
To avoid global variables in JavaScript code I many times seen that people use this construct:
(function($) {
// here code
// here code
})(jQuery);
So I have questions: why we need to declare function argument as $ and why we need to pass jQuery object as argument?
You don't have to. You can name the function argument whatever you want it to be. It is just a common/best practice of people using jQuery since it is common to use $ as an alias to jQuery library object.
The reason why you should do it is because there are other libraries that use $ as an alias to their library objects. It is needed to avoid collisions with those libraries since function closure will ensure $ to be jQuery object inside the wrapper function.
Here's an example:
(function (myJqueryAlias) {
console.log(myJqueryAlias('document') === jQuery('document'));
})(jQuery);
With this you archive that in this block you still have $ as jQuery and you can still write stuff like "$("selector")" instead of "jQuery("selector")".
And outside this block the variable $ is still what it should be (for example you use another JSLib that uses the $ too)

what's mean: !function in javascript? [duplicate]

This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 9 years ago.
I have a simple question.
I've found this code, and i don't know this statement
!function ($) {
// (...)
}(window.jQuery);
why put ! before a function?
i've found this on bootstrap.js file, and i really want to know.
Thanks!
It is a duplicate as nnnnnn mentioned. What the code is doing is executing the anonymous function while passing window.jQuery as a parameter, which will be referenced as $ inside the function. This allows the use of $ to reference jQuery without conflicting with any other library that might use the dollar sign.
This is a more readable version of the code:
(function($){
// here, $ references jQuery and any variable or function
// declared here cannot be overridden outside of this function
})(window.jQuery)
The ! will always parse the statement as being true if the statement is not able to be parsed.
You can see this by using,
javascript:alert(!function(){}())
Where the resulting response is true

Javascript $ notation [duplicate]

This question already has answers here:
What is the purpose of the dollar sign in JavaScript?
(12 answers)
Closed 9 years ago.
Please can anyone explain what this javascript code exactly does.
$('#element_id').html(response.title);
I need to access the value of the element_id but I can't using document.getElementById.
Thanks
This code just calls a function named $ and access a method of the returned object.
It's probably jQuery code due to the selector string.
$('#element_id'): Returns a jQuery object for the element with the given ID.
.html(response.title): Sets the inner HTML of the DOM element to response.title.
The raw JavaScript would look like this:
document.getElementById("element_id").innerHTML = response.title;
This code uses probably JQuery. The $ is the basic function defined by JQuery. You can call it to get access to element using a special query language defined by JQuery.
It looks like jQuery, which is a Javascript library. The $('#element_id') creates a jQuery object for the element with the id element_id in the DOM. Then .html(response.title) will put the value of response.title as HTML inside the element.
$ probably refers to jQuery, one of the most frquently used JS libraries.
What this snippet basically does is setting the HTML content of the element with the id element_id to the title attribute of the response object.

Categories