What is the consequence of this bit of javascript?

前端 未结 4 1596
清酒与你
清酒与你 2020-12-06 02:16

I was looking at the jQuery UI code, and I found that every file starts with a construct like this:

jQuery.ui || (function($) {

My question

4条回答
  •  没有蜡笔的小新
    2020-12-06 02:57

    I think the logical OR is being done to allow for the file to be included (and evaluated) multiple times, and not clobber itself: If you load it again, jQuery.ui will be already defined, and it will not do anything.

    The file could be included multiple times by mistake if someone has lost track of script tags.

    As for the semicolon, I can only guess that this is a safeguard to make sure that the file also works if included in another file, even if the last statement there is not terminated by a semicolon.

    A spurious semicolon does not do harm, a missing semicolon might.

提交回复
热议问题