0

저는 Google 컴파일러를 처음 사용하고 몇 가지 문제에 부딪치고 있습니다. 첫 번째는 내 전처리 된 코드에서 빈 배열을 나중에 채울 변수에 설정하는 것입니다.하지만 컴파일 할 때 나중에 변수를 사용할 때 정의되지 않도록 변수를 모두 제거합니다.Google 컴파일러가 빈 배열을 변수에 할당하지 않습니까?

다음은 약간 수정 한 코드입니다. 나는 다음과 같은 플래그를 사용하고, 컴파일러 설정의

/** 
* Equal Heights 
* 
* @see https://css-tricks.com/equal-height-blocks-in-rows/ 
*/ 
goog.provide('EqualHeights'); 

(function($) { 

    // = Equalize columns on load and resize 
    equal_heights(); 

    $(window).resize(function() { 
     equal_heights(); 
    }) 

    var currentTallest = 0; 
    var currentRowStart = 0; 
    var rowDivs = new Array(); 

    function setConformingHeight(el, newHeight) { 

     // set the height to something new, but remember the original height in case things change 
     el.data("originalHeight", (el.data("originalHeight") == undefined) ? (el.height()) : (el.data("originalHeight"))); 
     el.height(newHeight); 
    } 

    function getOriginalHeight(el) { 

     // if the height has changed, send the originalHeight 
     return (el.data("originalHeight") == undefined) ? (el.height()) : (el.data("originalHeight")); 
    } 

    function equal_heights() { 

     // find the tallest DIV in the row, and set the heights of all of the DIVs to match it. 
     $('[data-equalizer-watch]').each(function() { 

      // "caching" 
      var $el = $(this); 
      var topPosition = $el.position().top; 

      if (currentRowStart != topPosition) { 

       // we just came to a new row. Set all the heights on the completed row 
       for(currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) setConformingHeight(rowDivs[currentDiv], currentTallest); 

       // set the variables for the new row 
       rowDivs.length = 0; 

       // empty the array 
       currentRowStart = topPosition; 
       currentTallest = getOriginalHeight($el); 
       rowDivs.push($el); 
      } 
      else { 

       // another div on the current row. Add it to the list and check if it's taller 
       rowDivs.push($el); 
       currentTallest = (currentTallest < getOriginalHeight($el)) ? (getOriginalHeight($el)) : (currentTallest); 
      } 
     }); 

     // do the last row 
     for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) { 
      setConformingHeight(rowDivs[currentDiv], currentTallest); 

     } 
    } 

})(jQuery); 

: 선 (19)는 그렇게 때문에 rowDivs 라인 (44)에 정의되지 않은 게재되어 제거되는 끝납니다. 아마 뭔가를 분명 실종

--closure_entry_point main.js 
--externs node_modules/google-closure-compiler/contrib/externs/jquery-1.9.js 
--language_in ECMASCRIPTS5 
--warning_level: VERBOSE 

을하지만, 단지 궤도에 싶어 : 나는 심지어 고급 최적화 합병증 레벨을 사용하지하고 있습니다. 감사.

답변

0

단순 컴파일 모드에서는 지역 변수가 최소화되고 사용하지 않으면 제거 될 수 있습니다. 아마 무슨 일이 일어 났을까요? 당신이 지정한 엔트리 포인트를 기반으로 클로저 컴파일러는 rowDivs이 결코 사용할 수 없다고 결정합니다.

해결하려면 closure 컴파일러에 추가 정보를 알려줘야합니다.

(면책 조항 : 내가 jquery를 사용한 적이 없기 때문에 나는 다음과 같은 것을 추적 할 방법이 없을 수도있다. 나는 또한 노드 나 모듈을 사용하지 않았으며, 내가 익숙하지 않은 것들을 쓸 수있는 특별한 방법이있다. .) 시도

한 가지 JQuery와의 특별한 플래그입니다 : --process_jquery_primitives 당신이 당신의 함수에 전달할 필요가 없습니다 있도록 jquery-1.9 extern는 전역 변수로 $를 정의 https://github.com/google/closure-compiler/wiki/jQuery-Expansions

를 참조하십시오.

정의한 함수를 내보내려면 closure-compiler에게 알려야 할 수도 있습니다. "보관하고 싶은 심볼 내보내기"를 참조하십시오. https://developers.google.com/closure/compiler/docs/api-tutorial3

"객체 지향 스타일"을 사용하는 데 열심이 있다면 몇 가지 아이디어가 있습니다. 아마도 객체 지향 스타일로 변경하지 않고 코드를 작성하는 방법이있을 수 있지만 그 스타일에 익숙하지 않아서 도움이되지 않습니다.

(나는 당신의 프로그램이 시작할 때 활성화 할 jQuery를 얻는 방법을 모르지만, 난이 어딘가에 시작 코드에서 일어나는 가정합니다.) 당신은 자기가이 코드를 설계처럼

것 같습니다 goog.require('EqualHeights')라고 할 때마다 실행되는 함수를 실행합니다. 그러나 실제로 EqualHeights 네임 스페이스에 대해 정의하지는 않습니다. 어쩌면이 대신 : 시동 코드의 다음

goog.provide('EqualHeights'); 

/** 
* @constructor 
*/ 
EqualHeights = function() { 
    this.currentTallest = 0; 
    this.currentRowStart = 0; 
    this.rowDivs = new Array(); 
}; 

/** 
* @param {!Element} el 
* @param {number} newHeight 
*/ 
EqualHeights.prototype.setConformingHeight = function(el, newHeight) { 

    // set the height to something new, but remember the original height in case things change 
    el.data("originalHeight", (el.data("originalHeight") == undefined) ? (el.height()) : (el.data("originalHeight"))); 
    el.height(newHeight); 
}; 

/** 
* @param {!Element} el 
* @return {number} 
*/ 
EqualHeights.prototype.getOriginalHeight = function(el) { 

    // if the height has changed, send the originalHeight 
    return (el.data("originalHeight") == undefined) ? (el.height()) : (el.data("originalHeight")); 
} 

/** 
* @return {undefined} 
*/ 
EqualHeights.prototype.equal_heights = function() { 

    // find the tallest DIV in the row, and set the heights of all of the DIVs to match it. 
    $('[data-equalizer-watch]').each(function() { 

     // "caching" 
     var $el = $(this); 
     var topPosition = $el.position().top; 

     if (this.currentRowStart != topPosition) { 

      // we just came to a new row. Set all the heights on the completed row 
      for(currentDiv = 0 ; currentDiv < this.rowDivs.length ; currentDiv++) 
       setConformingHeight(this.rowDivs[currentDiv], this.currentTallest); 

      // set the variables for the new row 
      this.rowDivs.length = 0; 

      // empty the array 
      this.currentRowStart = topPosition; 
      this.currentTallest = getOriginalHeight($el); 
      this.rowDivs.push($el); 
     } 
     else { 

      // another div on the current row. Add it to the list and check if it's taller 
      this.rowDivs.push($el); 
      this.currentTallest = (this.currentTallest < getOriginalHeight($el)) ? (getOriginalHeight($el)) : (this.currentTallest); 
     } 
    }); 

    // do the last row 
    for (currentDiv = 0 ; currentDiv < this.rowDivs.length ; currentDiv++) { 
     setConformingHeight(this.rowDivs[currentDiv], this.currentTallest); 

    } 
} 

이 있습니다

goog.require('EqualHeights'); 

var eh = new EqualHeights(); 

// = Equalize columns on load and resize 
eh.equal_heights(); 

$(window).resize(function(){eh.equal_heights();}); 

나는이 선이 무엇을하고 있는지 불분명 해요 :

var $el = $(this); 

this의 값은해야한다 EqualHeights 각 메소드 안에있는 객체.goog.bind 함수 (사용 가능한 경우)를 사용하여 this이 해당 함수에 대해 원하는 것임을 확인할 수 있습니다.

관련 문제