how is that the jquery function can start with '$'? [duplicate] - javascript

This question already has answers here:
What is the meaning of "$" sign in JavaScript
(8 answers)
Closed 8 years ago.
if you declare a function in javascript, you can't use the '$' character in the name, so how is that jquery is able to do so? For example:
function myFunction() {
$("#h01").html("Hello jQuery")
}
$(document).ready(myFunction);
but if i declare a function as so:
function $(a){
// do something
}
javascript shows an error?

From the specification,
https://es5.github.io/#x7.6
IdentifierStart ::
UnicodeLetter
$
_
\ UnicodeEscapeSequence
$ is fine as a character in the name.
Demo, using the code in the question: http://jsfiddle.net/q02go7dd/

try
var $ = function(){
// do something
};
and you won't get an error.

$ is a short-handed alias for JQuery object itself.
So when you type,
$("#h01").html("Hello jQuery");
you are calling JQuery constructor with parameter "h01".
It's like declaring a function called dave and giving it an alias ß and calling it like that.
dave("h01");

$ is fine :
$myFunction = function(){ alert("hello"); }
$myFunction(); // alerts "hello"

Jquery is a wrapper which runs over javascript. In spite of using javascript large syntax, Jquery offer small syntaxs which is easy to use.
In Jquery $ sign return Jquery object.

Related

Javascript: What does the $ before the function mean? [duplicate]

This question already has answers here:
What is the purpose of the dollar sign in JavaScript?
(12 answers)
Closed 2 years ago.
What does the $ before the function mean in Javascript? I found this in a Javascript script for a Chromium extension.
$(function() {
$('#search').change(function() {
$('#bookmarks').empty();
dumpBookmarks($('#search').val());
});
});
This looks like jQuery but could also be any number of web frameworks.
$ is simply a variable (in this case a function) name, just like any other JavaScript variable name. It likely is also available and aliased as jQuery.
You can confirm and see the version using the below in a browser's developer console while on the website.
$ === jQuery
> true
$.fn.jquery
> "1.12.1"
It means it is a function of a JQuery object, a popular javascript library
This could be an Immediately-invoked Function Expressions with jQuery. $ is equal to 'jQuery'. With the immediate function, you can define variables inside the context of the function and avoid global variable definitions. As soon as jQuery loads this function executes.

Understanding the code structure [duplicate]

This question already has answers here:
What does (function($) {})(jQuery); mean?
(6 answers)
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 7 years ago.
What means and what will do this piece of code?
(function ($) {}(jQuery));
What is relation between "$" and "jQuery".
Do brackets "(", ")" on edges of code has any function? Do they do something?
(function ($) {}(jQuery));
jQuery is existing jQuery object , $ is same jQuery object within Immediately-Invoked Function Expression (IIFE) statement {}
Do brackets "(", ")" on edges of code has any function? Do they do
something?
Yes. Comma , separates arguments to function
e.g.,
(function($, $$) {
// `$`:`{"abc":123}` ; `$$`:`{"def":456}`
console.log($["abc"], $$["def"]) // `123` , `456`
// set `$` within IIFE to object `{"abc":123}` ,
// set `$$` to object `{"def":456}`
}({"abc":123}, {"def":456}));
What is relation between "$" and "jQuery"?
$ is shorthand for jQuery. Sometimes $ is disabled as it may conflict with other Javascript libraries you are using. It is otherwise identical.
and refer this
http://api.jquery.com/jQuery.noConflict/

how does jquery make the $ do what it does? [duplicate]

This question already has answers here:
Immediate function invocation syntax
(3 answers)
Closed 9 years ago.
In order to use the $ symbol in jquery and not have to use jQuery.functionname, we use this
(function($) {
})(jQuery);
(In drupal, you actually have to specify this implicitly).
I don't understand this javascript syntax, why is there an initial parentheses? How is the (jQuery) at the end used?
It's just an anonymous function with an argument that's automatically invoked.
For example, if we were to expand it out a bit you'd end up with something like this:
var anon = function($) {
...
};
anon(jQuery);
The $ is a valid identifer in JavaScript and we pass in the existing jQuery object into the function for use through $, as it could be replaced later.
All that's doing is declaring an anonymous function and executing it immediately, passing in one argument (jQuery) into the function. That argument is given the name $ which can be used throughout the scope of the function.
The brackets around the function aren't strictly necessary in all contexts; see the comment under this answer for details. The gist is that they're needed here to make the function behave like an expression instead of a statement (function declaration).

meaning of $ sign in jquery document ready [duplicate]

This question already has answers here:
jQuery dollar sign ($) as function argument?
(4 answers)
Closed 9 years ago.
Normally when I am writing my jquery code I do something like
$(document).ready(function() {
// some code
});
I was looking at some code online and I noticed that the author did this
$(document).ready(function($) {
// some code
});
What is the use of the $ as the function parameter
jQuery calls the callback function with jQuery as the first argument. Javascript doesn't require you to define parameters that will be passed to your function so it's usually left out if it's not needed.
Here it seems weird because the author is already relying on $ being jQuery - you would normally expect it to be along the lines of:
jQuery(document).ready(function($) {
// $ works here even if someone changed the global `$`
// this breaks down if someone changed jQuery too but that's far less likely
});
The jQuery function is the value of jQuery or of $. It serves as a namespace so we can call it "The global jQuery object."
Buddy Please jquery documentation.Its well written and easy to understand . Any way i will tell you what is the $ sign. $ is a shortcut to the jQuery function. .
**$**(document).ready(function() {
// statements
});
Here $ represents jquery .You can use jquery instead of $ sign..
See this link Click here

what does !function in Javascript mean? [duplicate]

This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 5 years ago.
Sorry for posting this but !function is not google-able and I did not find it in my JavaScript code.
Here is how Twitter uses it:
Tweet
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
from https://twitter.com/about/resources/buttons#
It is short-hand or alternative of self-invoking anonymous function:
(function(){
// code
})();
Can be written:
!function(){
// code
}();
You can also use + instead of !.
If you simply did:
function(){
// code
}();
That will create problems, that's why you need to add ! before it which turns the function declaration into function expression.
Quoting docs, section 12.4:
An ExpressionStatement cannot start with the function keyword because
that might make it ambiguous with a FunctionDeclaration.
To better understand the concept, you should check out:
Function Declarations vs. Function Expressions
they're negating the result, not the function itself:
!function( x ){ return x }( true );
!true
false
In reality, it's a slightly compressed form of:
(function(){}())
since it requires 1 less character. The reason it's needed is that you can't call a function declaration directly, e.g. this is invalid:
function(){}()
but adding the ! at the beginning turns it into a function expression and makes it work.
It's usually used to work around a quirk in the JavaScript syntax. This gives a syntax error:
function() {
}();
It's read as a function declaration (like function foo () {}), rather than a function expression. So adding the ! before it, or wrapping it in parentheses, forces the parser to understand that it's an expression.

Categories

Resources