这段 javascript 的结果是什么? [英] What is the consequence of this bit of javascript?

查看:23
本文介绍了这段 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆