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.

5
  • Did you search for the answer? This gets asked a lot. Commented Dec 9, 2013 at 6:43
  • You can find an answer for each of your questions if you search for it. Commented Dec 9, 2013 at 6: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? Commented Dec 9, 2013 at 6:46
  • Function syntax ... Leading semicolon ... Use strict Commented Dec 9, 2013 at 6: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. Commented Dec 9, 2013 at 6:48

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.