【问题标题】:Google Compiler not assigning empty arrays to variables?谷歌编译器没有将空数组分配给变量?
【发布时间】:2016-12-28 18:54:01
【问题描述】:

所以我对使用 Google 编译器非常陌生,并且遇到了一些问题。首先是在我的预处理代码中,我将一个空数组设置为稍后将填充的变量,但是在编译它时会完全删除该变量,以便稍后尝试使用它时它变得未定义。

这是我稍作修改的代码。第 19 行最终被删除,因此 rowDivs 在第 44 行显示为未定义:

/**
 * 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);

对于编译器设置,我使用以下标志。请注意,我什至没有使用高级优化复杂级别:

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

我可能遗漏了一些明显的东西,但只是想走上正轨。谢谢。

【问题讨论】:

    标签: google-closure-compiler google-closure-library code-elimination


    【解决方案1】:

    在简单编译模式下,局部变量被最小化,如果不使用,可能会被删除。可能发生的情况是:根据您指定的入口点,闭包编译器确定 rowDivs 永远无法使用。

    要修复,您需要告诉闭包编译器更多信息。

    (免责声明:我从未使用过 jquery,因此我可能会在接下来的内容中偏离轨道。我也从未使用过节点或模块,并且对于我不熟悉的那些有特殊的编写方法。 )

    要尝试的一件事是 jquery 的特殊标志:--process_jquery_primitives 请参阅 https://github.com/google/closure-compiler/wiki/jQuery-Expansions

    jquery-1.9 extern$ 定义为全局变量,因此您无需将其传递给您的函数。

    您可能需要告诉闭包编译器导出您定义的函数。请参阅“导出要保留的符号”中的 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 是您想要的那个函数。

    【讨论】:

      猜你喜欢
      • 2013-07-03
      • 2016-12-12
      • 2013-08-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 2020-11-01
      • 1970-01-01
      相关资源
      最近更新 更多