Understanding the code structure [duplicate] - javascript

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/

Related

JS function inside a parenthesis with a parenthesis at the end with an argument, what does it mean? [duplicate]

This question already has answers here:
What is the purpose of a self executing function in javascript?
(21 answers)
Closed 6 years ago.
What does this code do? Can someone describe why the function is inside a parenthesis and also why it has a parenthesis at the end and what it is doing?
(function (innerKey) {
//doSomething
}(key));
You are creating the function and invoking it at the same time with the key value filling the innerkey parameter.
It's a self-invoking anonymous function. It will be called immediately after loading the script, and it will take the element inside the brackets key as the function's argument.
You can read more here:
What is the (function() { } )() construct in JavaScript?

What (jQuery) means in the end of function? [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
(10 answers)
Closed 7 years ago.
I have a function:
var waitingDialog = (function($){
.....
return{
}
})(jQuery);
Also could you explain what $ means in the function? Is it going to work without that?
It means that jQuery (if it exists) will be passed to the function. $ is merely the name that variable will take in the scope of the function.
There is a section about that in official jQuery website : https://learn.jquery.com/plugins/basic-plugin-creation/#protecting-the-alias-and-adding-scope

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

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.

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

Why need to use semicolon before defining a function? [duplicate]

This question already has answers here:
What does the leading semicolon in JavaScript libraries do?
(6 answers)
What's the purpose of starting semi colon at beginning of JavaScript? [duplicate]
(2 answers)
Closed 9 years ago.
I've seen some strange ; at the beginning of a function in some jQuery plugins source code like this:
;(function ($) {.....
Can someone explain why they need to use ; in this case?
This semicolon will help you to properly concatenate a new code into a file when the current existed code in this file does not include a ; at the end.
For example:
(function() {
})() // <--- No semicolon
// Added semicolon to prevent unexpected laziness result from previous code
;(function ($) {
})();
Without the semicolon, the second () would have been interpreted as a function call, and will tried to call the return result of the first function
This is just to make sure to terminate any previous instruction.
the semi colon before function invocation is a safety net against
concatenated scripts and/or other plugins which may not be closed
properly.
https://github.com/shichuan/javascript-patterns/blob/master/jquery-plugin-patterns/extend.html

Categories

Resources