2011-08-05 4 views
4

가능한 중복 :.
What is the consequence of this bit of 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: { 
... 
+6

참조 http://stackoverflow.com/questions/1031718/what-is-the-consequence-of-this-bit-of-javascript – JJJ

답변

1

내려 속보 :

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

편집 : 여러 소스 파일이 축소 된 경우 What is the consequence of this bit of javascript?

  • 리딩 세미콜론 속는 사람은 이전 문이 닫혀 있는지 확인하는 것입니다 하나에.

  • jQuery.ui || 비트는 jQuery.ui이 아직 존재하지 않는 경우에만 다음 기능이 정의되도록합니다.

1

자바 스크립트 || true로 평가되면 첫 번째 값을 사용하고 첫 번째 값이 false로 평가되면 두 번째 값을 사용합니다.

이 경우 jQuery.ui이 있는지 여부를 확인하고 그렇지 않은 경우 익명 함수를 평가합니다. jQuery.ui가 존재하면 || 두 번째 값을 평가하지 않으므로 익명의 함수가 실행되지 않습니다.