I am trying to redefine Array.prototype.indexOf for old IE versions. I am having trouble typing it correctly according to Google Closure Compiler.

It says that the type of @this is wrong.

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

I get the following output

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) {
^

Surprisingly, changing @this {Array} by @this {Object} (though it does not make much sense) returns this even more obscure message:

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) {
^

Any hint on how to do it properly?

有帮助吗?

解决方案

You can use @suppress {duplicate} to ignore this warning:

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

I am not sure about the implications redefining the method has on optimizations from the Closure Compiler in ADVANCED mode, though.

其他提示

Array methods are generic, they should in fact take an Array-like value. The latest Closure Compiler defines it as:

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

Simply assigning the value works:

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

Consider upgrading to a recent release of the Compiler.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top