2012-12-13 41 views
0

我在common.js文件中有以下方法。當我將「common.js」文件包含到打印文件中時,第一行出現編譯錯誤。Overloding Function.prototype時編譯錯誤

我應該如何解決這個問題?

Function.prototype.method = function (name, func) { 
    ///<summary> 
    ///Create a new method if it not ready exists 
    ///</summary> 
    if (!this.prototype[name]) { 
     this.prototype[name] = func; 
     return this; 
    } 
}; 

String.method('trim', function() { 
    return this.replace(/^\s|\s+$/g, ''); 
}); 

回答

2

找到它了。

只需將common.js文件移動到common.ts文件即可。然後在延伸'原型'的行之前添加以下代碼:

interface Function { 
    method(name: string, func: Function): any; 
}