我試圖重新定義舊版IE的Array.prototype.indexOf
。根據Google Closure Compiler,我無法正確輸入。鍵入indexOf使用Closure編譯器重新定義
它說@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 {Object}
@this {Array}
(儘管它沒有多大意義)返回此更加晦澀消息:
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) {
^
任何暗示如何正確地做到這一點?
謝謝,就是我在找的東西! –
重新定義應該沒問題,只要簽名與原件完全匹配,並且該功能不會導致副作用(因爲原件沒有)。這就是其他警告的原因。 –