【问题标题】:Typing indexOf redefinition with Closure compiler使用 Closure 编译器键入 indexOf 重新定义
【发布时间】:2013-09-04 01:26:16
【问题描述】:

我正在尝试为旧的 IE 版本重新定义 Array.prototype.indexOf。根据 Google Closure Compiler,我无法正确输入。

@this的类型不对。

if (!Array.prototype.indexOf) {
  /**                                                                                                                                                 
   * @this {Array}                                                                                                                                    
   * @param {*} item                                                                                                                                  
   * @param {number=} from: ignored 
   * @return {number}                                                                                                                                 
   */
  Array.prototype.indexOf = function(item, from) {
    // ...
  }
}

我得到以下输出

test.js:12: WARNING - variable Array.prototype.indexOf redefined with type \
function (this:Array, *, number=): number, original definition at \
externs.zip//es3.js:633 with type function (this:Object, *, number=): number
Array.prototype.indexOf = function(item, from) {
^

令人惊讶的是,将@this {Array} 更改为@this {Object}(尽管没有多大意义)会返回这个更加模糊的消息:

test.js:12: WARNING - variable Array.prototype.indexOf redefined with type \
function (this:Object, *, number=): number, original definition at \
externs.zip//es3.js:633 with type function (this:Object, *, number=): number
Array.prototype.indexOf = function(item, from) {
^

关于如何正确执行的任何提示?

【问题讨论】:

    标签: javascript google-closure-compiler google-closure


    【解决方案1】:

    您可以使用@suppress {duplicate} 忽略此警告:

    /**
     * @this {Array}
     * @param {*} item
     * @param {number=} from: ignored
     * @return {number}
     * @suppress {duplicate}
     */
    Array.prototype.indexOf = function(item, from) {
        // ...
    }
    

    不过,我不确定重新定义方法对高级模式下闭包编译器优化的影响。

    【讨论】:

    • 谢谢,正是我想要的!
    • 重新定义应该没问题,只要签名与原始签名完全匹配并且该功能不会引起副作用(因为原始没有)。这就是另一个警告的内容。
    【解决方案2】:

    Array 方法是通用的,它们实际上应该采用类似 Array 的值。最新的 Closure Compiler 将其定义为:

    /**
     * Available in ECMAScript 5, Mozilla 1.6+.
     * @param {T} obj
     * @param {number=} opt_fromIndex
     * @return {number}
     * @this {{length: number}|Array.<T>|string}
     * @nosideeffects
     * @template T
     * @see http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf
     */
    Array.prototype.indexOf = function(obj, opt_fromIndex) {};
    

    简单地赋值就可以了:

    if (!Array.prototype.indexOf) {
      Array.prototype.indexOf = function(item, from) {
         // ...
      }
    }
    

    考虑升级到最新版本的编译器。

    【讨论】:

    • 谢谢,但简单地分配值会返回相同的警告source.js:488: WARNING - variable Array.prototype.indexOf redefined with type function (this:Array, *, number=): number, original definition at externs.zip//es3.js:633 with type function (this:Object, *, number=): number。我使用的是几周前下载的 2508M 版本。我相信这是最新的。我收到警告的原因可能是因为我使用 "reportUnknownTypes = CheckLevel.WARNING" 编译以进行严格输入。
    • 报告未知类型不会影响这一点。您的编译器版本很可能是旧的。
    • 这很奇怪,我的构建来自一周前运行 svn checkout http://closure-compiler.googlecode.com/svn/trunk/ closure-compiler。版本为2508M。
    • 闭包编译器不久前移至 git:code.google.com/p/closure-compiler/source/checkout
    • 谢谢!我为stackoverflow.com/questions/4230326/… 添加了一个新答案,这样其他人就不会坚持使用旧的存储库。
    猜你喜欢
    • 2011-01-14
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    相关资源
    最近更新 更多