0

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.

zipzit
  • 3,778
  • 4
  • 35
  • 63
  • Did you search for the answer? This gets asked *a lot*. – Blue Skies Dec 09 '13 at 06:43
  • You can find an answer for each of your questions if you search for it. – Felix Kling Dec 09 '13 at 06:46
  • Hmmm.. I did 20 or so searches in google, four or five in Bing. Are you telling me that these are specific stack overflow searches? – zipzit Dec 09 '13 at 06:46
  • [Function syntax](http://stackoverflow.com/questions/16104993/what-is-this-syntax-function-undefined) ... [Leading semicolon](http://stackoverflow.com/questions/16319510/why-need-to-use-semicolon-before-defining-a-function) ... [Use strict](http://stackoverflow.com/questions/1335851/what-does-use-strict-do-in-javascript-and-what-is-the-reasoning-behind-it) – Blue Skies Dec 09 '13 at 06:47
  • *"Why is the 'use strict'; code there, if this is a jQuery code."* jQuery is just a library. You are still writing JavaScript, using that library. No matter which library you use, `"use strict"` can be useful. – Felix Kling Dec 09 '13 at 06:48

1 Answers1

0

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/

Bas van Dijk
  • 9,933
  • 10
  • 55
  • 91