这段 javascript 的结果是什么? [英] What is the consequence of this bit of javascript?
问题描述
我正在查看 jQuery UI 代码,我发现每个文件都以这样的结构开头:
I was looking at the jQuery UI code, and I found that every file starts with a construct like this:
jQuery.ui || (function($) {
我的问题是:为什么 jQuery
前面有一个分号,为什么要进行逻辑 OR?
My question is: why is there a semicolon before jQuery
, and why is the logical OR being done?
推荐答案
为什么jQuery前面有一个分号?
why is there a semicolon before jQuery?
分号是为了确保安全的文件连接.(库和库组件经常被打包成一个文件)
The semi-colon is there to ensure safe file concatenation. (libraries and library components are frequently packed into a single file)
为什么要进行逻辑 OR?
why is the logical OR being done?
右侧的自调用匿名函数只有在语句左侧的计算结果为假值时才会运行.因此,如果 jQuery.ui
已经存在于页面上,那么该函数将不会运行.它仅在 jQuery.ui
尚不存在时运行.
The self-invoking anonymous function on the right hand-side will only run if the left-hand-side of the statement evaluates to a falsey value. So if jQuery.ui
already exists on the page then the function will not run. It only runs when jQuery.ui
does not yet exist.
这篇关于这段 javascript 的结果是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!