使用闭包编译器键入indexOf重新定义
Typing indexOf redefinition with Closure compiler
我正在尝试为旧IE版本重新定义Array.prototype.indexOf
。根据谷歌闭包编译器,我在正确键入它时遇到了问题。
它说@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) {
^
有什么关于如何正确做的提示吗?
您可以使用@suppress {duplicate}
忽略此警告:
/**
* @this {Array}
* @param {*} item
* @param {number=} from: ignored
* @return {number}
* @suppress {duplicate}
*/
Array.prototype.indexOf = function(item, from) {
// ...
}
不过,我不确定在ADVANCED模式下重新定义该方法对闭包编译器优化的影响。
数组方法是通用的,它们实际上应该采用类似于Array的值。最新的闭包编译器将其定义为:
/**
* 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) {
// ...
}
}
请考虑升级到最新版本的编译器。
相关文章:
- 在Phonegap中重定向到新页面后,AdMob未定义
- $routeParams在传递到新视图时未定义&控制器
- 使用具有用户定义字段的新请求者创建新的Zendesk票证
- 为什么这个功能打印“;未定义的未定义的“;作为联系人名称,添加“新对象”
- 新的自定义角度指令不起作用
- 正在新文件中创建自定义任务
- 在自定义指令中角度设置新变量
- Angular2 在创建 ngSwitch 新视图后调用自定义函数
- Ng管理员自定义参考列表-具有创建新条目的能力
- JavaScript-为什么当我向表中添加新列时,它会说“;未定义的“;在额外的细胞中
- 扩展或定义新的dat.gui属性控制器的最佳方法
- 使用 RequireJS 加载时如何不定义“定义”
- 在数据表的新行中添加按钮,变得未定义
- 从新对象的属性更新对象的属性,但前提是新对象上的属性不是未定义的
- 创建新的子类对象时设置父类中定义的变量时出现问题
- 创建 JavaScript 自定义应用程序的新实例
- 如何观看 img 元素的 ngSrc 并在自定义指令中获取新的宽度和高度
- Chrome扩展抛出"未定义"定义变量
- 如何将自定义(定义)顺序应用于DC.js中的行图的行
- Javascript/JQuery POST "url"未定义(定义)