3

我正在使用以下函数来修改 Javascript 数组的特定实例的行为。如何注释 Closure Compiler 的代码? http://code.google.com/closure/compiler/docs/js-for-compiler.html通过编译器运行代码会产生“JSC_USED_GLOBAL_THIS”错误。

function listify(array) {
    array.toString = function() {
        return '[' + this.join(', ') + ']';
    };
    return array;
};

看起来我不能使用 @extends 或 @constructor 注释。

我不想修改全局 Array 原型,因为在页面上使用其他代码时会产生意想不到的副作用。此外,在阅读http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/之后,我认为我的方法是我用例的最佳方法。问题是我只是不知道如何将它注释到编译器

4

1 回答 1

3
function listify(array) {

    /**
     * Returns the roster widget element.
     * @this {Array}
     * @return {String}
     */
    array.toString = function() {
        return '[' + this.join(', ') + ']';
    };
    return array;
};
于 2011-11-21T20:24:25.903 回答