它有什么作用? ; jQuery.ui || (函数($){

可能重复:
这个javascript的结果是什么?

我正在浏览JQuery UI的源代码。 我在js文件的开头看到了这一行:

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

它有什么作用?

(更多来自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: { ... 

打破它:

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

编辑:Dupe of 这个javascript的结果是什么?

  • 前导分号用于确保在将多个源文件缩小为一个时关闭任何先前的语句。

  • jQuery.ui || bit确保仅在jQuery.ui尚不存在时才定义以下函数。

javascript || 如果计算结果为true,则使用第一个值;如果第一个计算结果为false,则使用第二个值。

在这种情况下,我假设它检查jQuery.ui是否存在,如果不存在,那么它将评估匿名函数。 如果jQuery.ui确实存在那么|| 不会评估第二个值,因此不会运行匿名函数。