4

Possible Duplicate:
What is the consequence of this bit of javascript?

I was browsing the source code of JQuery UI. I saw this line at the beginning of the js file:

;jQuery.ui || (function($) {

what does it do?

(more from the jquery.ui.core.js)

/*!
 * jQuery UI 1.8
 *
 * Copyright (c) 2010 AUTHORS.txt (http://jqueryui.com/about)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://docs.jquery.com/UI
 */
;jQuery.ui || (function($) {
.ui = {
    version: "1.8",

    // $.ui.plugin is deprecated.  Use the proxy pattern instead.
    plugin: {
...
Community
  • 1
  • 1
AlexStack
  • 16,766
  • 21
  • 72
  • 104
  • 6
    See http://stackoverflow.com/questions/1031718/what-is-the-consequence-of-this-bit-of-javascript – JJJ Aug 05 '11 at 11:52

3 Answers3

2

Edit: Dupe of What is the consequence of this bit of javascript?

  • The leading semicolon is to make sure any previous statements are closed when multiple source files are minified into one.

  • The jQuery.ui || bit makes sure that the following function is defined only if jQuery.ui does not already exist.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
2

Breaking it down:

// make sure that any previous statements are properly closed
// this is handy when concatenating files, for example
; 
// Call the jQuery.ui object, or, if it does not exist, create it
jQuery.ui || (function($) { 
Aron Rotteveel
  • 81,193
  • 17
  • 104
  • 128
1

The javascript || will use the first value if it evaluates as true and will use the second if the first evaluates as false.

In this case I presume it checks whether jQuery.ui exists and if it does not then it will evaluate the anonymous function. If jQuery.ui does exist then || will not evaluate the second value and so the anonymous function will not be run.

alnorth29
  • 3,525
  • 2
  • 34
  • 50