0
我是012,的新手,並且嘗試使用javascript代碼打入打字稿,所以我最終得到了下面的代碼。 關於打字稿中類別的理解如下,可能是錯誤的: 1.定義完類後,必須聲明稍後要使用的參數this
,減速的格式爲variableName:variableType
。 2.在構造中,變量可根據class
該變量可以與this.
class decleration - TypeScript中不存在屬性
可以使用在下面的代碼作爲this.variableName = x
3.分配值在methods
,我得到了錯誤[ts] Property 'escapeRegExp' does not exist on type 'typeof Listen'.
所示附接在圖像。
namespace CORE{
export class Listen{
commandsList:[RegExp, string, string];
debugStyle:string;
optionalParam:RegExp;
optionalRegex:RegExp;
namedParam:RegExp;
splatParam:RegExp;
escapeRegExp:RegExp;
constructor(){
this.commandsList = [];
this.debugStyle = 'font-weight: bold; color: #00f;';
this.optionalParam = /\s*\((.*?)\)\s*/g;
this.optionalRegex = /(\(\?:[^)]+\))\?/g;
this.namedParam = /(\(\?)?:\w+/g;
this.splatParam = /\*\w+/g;
this.escapeRegExp = /[\-{}\[\]+?.,\\\^$|#]/g;
}
public static commandToRegExp(command:string):RegExp{
command = command.replace(this.escapeRegExp, '\\$&')
.replace(this.optionalParam, '(?:$1)?')
.replace(this.namedParam, function(match, optional) {
return optional ? match : '([^\\s]+)';
})
.replace(this.splatParam, '(.*?)')
.replace(this.optionalRegex, '\\s*$1?\\s*');
return new RegExp('^' + command + '$', 'i');
}
public static registerCommand(command:RegExp, cb:string, phrase:string):void{
this.commandsList.push({ command: command, callback: cb, originalPhrase: phrase });
}
public static addCommands(commands:string[]):void{
var cb;
for (var phrase in commands) {
if (commands.hasOwnProperty(phrase)) {
cb = this[commands[phrase]] || commands[phrase];
if (typeof cb === 'function') {
// convert command to regex then register the command
this.registerCommand(this.commandToRegExp(phrase), cb, phrase);
} else if (typeof cb === 'object' && cb.regexp instanceof RegExp) {
// register the command
this.registerCommand(new RegExp(cb.regexp.source, 'i'), cb.callback, phrase);
}
}
}
}
public static executeCommand(commandText:string):void{
for (var j = 0, l = this.commandsList.length; j < l; j++) {
var result = this.commandsList[j].command.exec(commandText);
if (result) {
var parameters = result.slice(1);
// execute the matched command
this.commandsList[j].callback.apply(this, parameters);
}
}
}
}
}
那麼,我應該做些什麼修正,你可以給一些代碼行。謝謝 –
我個人建議學習靜態方法和類方法之間的區別,但快速解決方法是從你的方法中去除'static'。我會編輯我的答案。 – PaulBGD
erorr消失了,所以我會將您的答案標記爲正確的解決方案,但看起來我在這裏有另一個錯誤:http://stackoverflow.com/q/41316133/2441637 –