Sunday, April 3, 2011

JQuery ui.core.js

I found the beginning of the code of ui.core.js of JQuery UI quite interesting,

;jQuery.ui || (function($) {
// code...
})(jQuery);

What is the reason to have ';' in the front?

From stackoverflow
  • It is there for concatenation purposes in case one would want to concatenate this file at the end of another script. It effectively fool-proofs concatenation to scripts that have not been terminated properly by a semi-colon.

    So, given the following script:

    alotOfJsCode(argument);
    var fileEnd = noSemiColon
    

    The semi-colon at the beginning allows to prevent this:

    alotOfJsCode(argument);
    var fileEnd = noSemiColonjQuery.ui || (function($) { //...
    

    Which would cause the code to fail.

    In JavaScript, a semi-colon all by itself has no syntactic value. The following two statements are the same:

    //Statement 1
    ;;; ;; ; alert('hello world!'); ;;; ;; ;;
    
    //Statement 2
    alert('hello world!');
    
    musicfreak : Huh, I didn't know that was valid JavaScript. +1

0 comments:

Post a Comment