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

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

Related

Why doesn't JavaScript require semicolons after function declarations? [duplicate]

This question already has answers here:
Why should I use a semicolon after every function in javascript?
(9 answers)
Closed 7 years ago.
A student asked me why JavaScript requires semicolons after variable declarations but not after function declarations and I didn't really have a good answer.
For example, these variable declarations (including the one holding a function) are followed by semicolons...
var x = 5;
var test = function() { return null; };
But this function declaration has no semicolon afterwards nor should it. Why? What is the logic behind the differentiation? Why does variable assignment require a semicolon but function declaration does not?
function test {
return null;
}
Semicolons serve to separate statements from each other, and a FunctionDeclaration is not a statement.
Well, semicolons after function deceleration aren't required.
The answer is pretty simple.
Semicolons are used in JavaScript on order to separate statements.
So function declarations aren't statements.
Edit:
Oh, #Basil Baby, was faster :P

Unnamed function in JavaScript doesn't work [duplicate]

This question already has answers here:
jQuery document.ready vs self calling anonymous function
(5 answers)
Closed 8 years ago.
I have tested something and it was really strange...
When I use:
jQuery(document).ready(function ($){
console.log($('.box').length);
});
the return value is 4;
If I use this:
(function ($){
console.log($('.box').length);
})(jQuery);
the return value is 0;
(in the same document)
Any explanation for that?
(I have tried to reproduce in jsfiddle but both return same value.)
Your second example will cause the code in the function to run when the whole overall statement is parsed. The jQuery version waits until the DOM has been fully parsed and populated. The two pieces of code are simply different, in other words.

Obtuse Javascript / jQuery coding style? [duplicate]

This question already has answers here:
What does the leading semicolon in JavaScript libraries do?
(6 answers)
What does this JavaScript/jQuery syntax mean?
(6 answers)
Closed 9 years ago.
I'm trying to understand an unusual library for controlling 3D CSS navigation. I'm reviewing the code, but I just don't understand the style.
The javascript code starts
;(function($) {
'use strict';
. . .
})(jQuery);
1) I'm really baffled by the leading semicolon, is there a reason for that?
2) I've never seen the format: (function($) { What am I looking at? Is this some sort of obtuse jquery format? I've seen lots of other formats relating to jquery.. e.g..
$(function() { // as shorthand for $( document ).ready()
but I've never seen (function($) before.. am I missing something?
3) Why is the 'use strict'; code there, if this is a jQuery code. Seems unusual.
4) Finally why is the {jQuery) code at the end of the function?
Oh, and for reference the code I'm looking at is http://www.jqueryscript.net/demo/Easy-jQuery-3D-Side-Menu-Plugin-with-CSS3-Box-Lid/
Many thanks, Zip.
1) This semicolon is here to make sure there will be no conflict when using minifiers, which adds all Javascript after each other. When combining multiple Javascript files it sometimes happens that a certain file has an error and "forgot" to end the last line with a semicolon. The semicolon makes sure the previous code line is ended.
2,4) (function($) { starts an anonymous function which is directly executed. What happens is the following. First we make an anonymous function like we always e.g.:
function($) {
}
In this function the $ is a function parameter. Now if we want to execute this function we need to enclose it in parentheses so it becomes:
(function($) {
});
Since we want to add the jQuery object as parameter for this functions we give it as a parameter like we do in every function:
(function($) {
})(jQuery);
3) Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a "strict" operating context. This strict context prevents certain actions from being taken and throws more exceptions.
Read more information about strict mode at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode and http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/

What does the first semicolon imply? " ;( function( ) { } ) ( ); " [duplicate]

This question already has answers here:
Semicolon before self-invoking function? [duplicate]
(4 answers)
Closed 9 years ago.
I was just going through the source code of cordova.js, its structure is like
;(function() { cordova code I have yet to understand })();
Just curious what does the first semi-colon imply?
Is it just to make sure that there is a semicolon preceding the anonymous function or does it mean something else?
It is a defensive semicolon, this is in case someone concatenates some JavaScript before your code, and this concatenated code forgot to put a terminating semicolon.
It's just to prevent an error if you combine multiple js files. So you can remove it if you want.

Is this jQuery related, and what does this mean? [duplicate]

This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 9 years ago.
I have been searching, but no clues... or I haven't search the proper way (so please excuse me if duplicate)
Does the following code, mean: If there is no jQuery defined, or no document ready?
!function ($) {
///
!function ($) {
$(function(){ // I know this is an alias to $(document).ready()
.....
}(window.jQuery) // Ending of !function
I'm asking, because I saw it here: http://twitter.github.io/bootstrap/assets/js/application.js and have no I really don't know what it means.
In this case, ! is being used because it's an operator, so the rest of the line will be treated as an expression rather than a statement. This is a way of writing an immediately invoked function expression. The more common idioms can be found here:
Javascript immediately invoked function patterns
! on a function(){}() simply flips (or negates) the value that's returned after immediately calling the function that's defined. Notice that immediately after the function definition, at the very last line, it says (window.jQuery) — that's passing jQuery as the argument to the function and calling it immediately.
But in this case it doesn't appear to do anything important since the return value won't be used anyway. The function will still be executed though.
Also, it says this at the top of the file:
// NOTICE!! DO NOT USE ANY OF THIS JAVASCRIPT
// IT'S ALL JUST JUNK FOR OUR DOCS!
// ++++++++++++++++++++++++++++++++++++++++++
So that's evidence further that it's not meant to serve any real purpose.

Categories

Resources