Obtuse Javascript / jQuery coding style? [duplicate] - javascript

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/

Related

What does "+" means in +function($)? [duplicate]

This question already has answers here:
JavaScript plus sign in front of function expression
(4 answers)
Closed 5 years ago.
I had look at this question to know about what this means.
(function($) {
})(jQuery);
I am looking at different bootstrap plugins, which have +function ($), while defining the function.
What does + does here, does it appends this function to other functions ?
To guide the javascript parser, that the thing written near the unary operator + is an expression.
EDIT: You can create javascript functions anonymously. This is one of the syntaxes to create the same. By doing so, when they are called (i.e evaluated), they act like a returning a function value. You can read more from the second link which provides a good description of it.
This link explains it well
Once declared, if not named, these can be executed inline like IIFE (Immediately invoked function expressions) as well. And in this form, they can then be used to create plugins or used as namespaces and attached to window object or jquery object for use later.
Good sample file to see anonymous function code in action
http://www.programering.com/a/MTMwITMwATk.html
It's a bang function
the + operator is faster than usual !
see more at
javascript function leading bang ! syntax

Why is the function with in parenthesis? [duplicate]

This question already has answers here:
Why is this function wrapped in parentheses, followed by parentheses? [duplicate]
(6 answers)
Closed 8 years ago.
Question:
Why is the function wrapped in a parenthesis? I have taken this code out of parenthesis and it works with no trouble.
What is the benefit of having the code in a (function() { ...Code Here...})(); like it is in the following example?
Code:
(function() {
"use strict";
// Doesn't work because strict mode
// enforces the fact that the second example shouldn't work
if (true) {
function fn1() {
// This won't run
console.log("doesn't work -- have a great Die Hard Day XIII");
};
fn1();
}
})();
Code Here: What would sending the JQuery word as a parameter do for this namespace. I know that the reason that the function is enclosed in (...) is to create a namespace. I guess a better question would be as to why one would pass in a variable, but I would imagine that would be in case another namespace needed the variable.
( function( $ ) {
// Init Skrollr
var s = skrollr.init({
render: function(data) {
//Debugging - Log the current scroll position.
//console.log(data.curTop);
}
});
} )( jQuery );
I have taken this code out of parenthesis and it works with no trouble.
That’s not correct; it can’t run (or even be parsed) by itself. JavaScript sees function in a place where a function declaration can be and assumes it’s a function declaration. Parentheses are used to force the context to be an expression. The practice is redundant if it’s unambiguously a function literal – say, in a variable declaration – but many find it more readable. There’s a jsHint option to enforce it, for example.
Because then they are calling it:
(function() { ... })();
^^

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.

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

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