How does Jquery use the dollar sign? [duplicate] - javascript

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
}

Related

Colon after colon syntax while calling a function in Javascript [duplicate]

This question already has an answer here:
Colon after function declaration in javascript [duplicate]
(1 answer)
Closed 4 years ago.
I came across a syntax somewhere on the web recently and couldn't grasp its meaning.
What I understand is that when we write props: Object inside the brackets, it means we're assigning a default value to props as Object. But what does the 2nd colon signify? It looks like a key-value pair but is confusing me still.
Tried searching on the web but wasn't able to search due to lack of terminology. Any ideas what this means?
someFn(props: Object): Object {
return someOtherFn(props);
}
These are type annotations. They are not standard javascript. They are added when using tools that layer static typing onto javascript. The two most popular flavors are Typescript and Flow.
When you write code that uses this syntax you will transpile your source code into code that is syntactically valid for execution by running one of the above mentioned tools on your code. When you do, it will tell you if your usage of the types is correct, raise warnings that are helpful in development, and then strip all this out so it can actually be run.

Where can i find the code source of javascript functions? [duplicate]

This question already has answers here:
Where can I find javascript native functions source code? [duplicate]
(3 answers)
Closed 1 year ago.
For example, i want to know how the querySelector method was built. I checked on MDN but there's only examples that show how to use it.
I also tried to check in the console.log but nothing readable.
There is a non-standard method to do this. Please be aware that it is not fully cross-browser compatible and shouldn't be used in a production environment.
function hello(){
return "Hi!";
}
console.log(hello.toSource());
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/toSource for more.
Edit:
To see the source code of a built-in function, you will need to look at the engine source. This varies browser to browser. For example, Chrome uses the V8 engine. See https://github.com/v8/v8

How to know when to use $variableName or just variableName? [duplicate]

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.

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

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).

What is the difference between "jQuery(selector)" and "$(selector)"? [duplicate]

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===$

Categories

Resources