开发者

What does it do? ;jQuery.ui || (function($) { [duplicate]

开发者 https://www.devze.com 2023-03-25 08:09 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: What is the consequence of this bit of javascript?
This question already has answers here: Closed 11 years ago.

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 fil开发者_运维知识库e:

;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: {
...


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($) { 


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消