2016-12-24 214 views
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); 
       } 
      } 
     }   
    } 
} 

enter image description here

回答

0

this不上的静态方法存在。静态方法不绑定到类实例。静态方法有很多阅读,但基本上:类方法可以调用静态方法,静态方法不能调用类方法(没有实例)。

此处的修复方法是从您的方法中删除static

+0

那么,我应该做些什么修正,你可以给一些代码行。谢谢 –

+0

我个人建议学习静态方法和类方法之间的区别,但快速解决方法是从你的方法中去除'static'。我会编辑我的答案。 – PaulBGD

+0

erorr消失了,所以我会将您的答案标记为正确的解决方案,但看起来我在这里有另一个错误:http://stackoverflow.com/q/41316133/2441637 –