2016-08-14 4 views
-2

私はTerminalクラスのoutput()関数にアクセスしたい無名関数を持つ関数コンストラクタを持つTerminalという名前のクラスを持っています。これどうやってするの?ファンクションinクラス

this.output()を使用するとエラーが発生します。 thisに等しいthat

class Terminal 
{ 
    constructor(contentID, inputID, options) 
    { 
     if (!contentID || !inputID || !options) 
     { 
      return false; 
     } 
     this.cid = contentID; 
     this.iid = inputID; 
     if (!options['pcName'] || !options['username']) 
     { 
      return false; 
     } 
     this.pcname = (options['pcName'].replace('<', '&lt;')).replace('>', '&rt;'); 
     this.username = options['username']; 
     this.version = '0.01'; 
     // commands 
     this.commandsList = []; 
     this.commandsList['whoami'] = function(){ Terminal.output('lol') }; 

     console.log('terminal.js version ' + this.version + ' initializing'); 
     this.output('ja', true); 
     this.output('whoami', true); 
     this.output('i dont know bro', false, 'whoami'); 
    } 

    output (text, prefix, cmdName) // cmdName is matters only when prefix is false 
    { 
     if (this.cid && text) 
     { 
      if (prefix == true) 
      { 
       text = this.username + '@' + this.pcname + ':~$ ' + text; 
      } 
      else if (cmdName) 
      { 
       text = cmdName + ': ' + text; 
      } 
      var con = document.getElementById(this.cid); 
      con.innerHTML = con.innerHTML + text + '<br>'; 
     } 
    } 

    makeCommand (cmd) 
    { 
     if (cmd && cmd != '' && this.commandsList) 
     { 
      cmd = (cmd.replace('<', '&lt;')).replace('>', '&rt;'); 
      if (this.commandsList[cmd]) 
      { 
       console.log('Executing ' + cmd); 
       this.commandsList[cmd](); 
      } 
     } 
    } 
} 
+0

this.output()、出力のように「これ」機能に特異的に結合することができます()とTerminal.output() - 結果なし。 – Dante383

+0

私たちにコード – Ivan

+0

を表示するhttp://pastebin.com/95seeX7xそれは、私が意味する混乱しているが匿名の機能は、21行目で、それは54で呼び出されます。 – Dante383

答えて

0

(this.outputは関数ではありません)私はそれが答えだわからない、私はあなたが新しい変数を定義する必要があると思います。そして、そのようなメソッドを呼び出す:that.Output()

class Terminal 
 
{ 
 
    constructor(contentID, inputID, options) 
 
    { 
 
     if (!contentID || !inputID || !options) 
 
     { 
 
      return false; 
 
     } 
 
     this.cid = contentID; 
 
     this.iid = inputID; 
 
     if (!options['pcName'] || !options['username']) 
 
     { 
 
      return false; 
 
     } 
 
     this.pcname = (options['pcName'].replace('<', '&lt;')).replace('>', '&rt;'); 
 
     this.username = options['username']; 
 
     this.version = '0.01'; 
 
     // commands 
 
     this.commandsList = []; 
 
     var that = this; 
 
     this.commandsList['whoami'] = function(){ that.output('lol') }; 
 
    
 
     console.log('terminal.js version ' + this.version + ' initializing'); 
 
     this.output('ja', true); 
 
     this.output('whoami', true); 
 
     this.output('i dont know bro', false, 'whoami'); 
 
    } 
 
    
 
    output (text, prefix, cmdName) // cmdName is matters only when prefix is false 
 
    { 
 
     if (this.cid && text) 
 
     { 
 
      if (prefix == true) 
 
      { 
 
       text = this.username + '@' + this.pcname + ':~$ ' + text; 
 
      } 
 
      else if (cmdName) 
 
      { 
 
       text = cmdName + ': ' + text; 
 
      } 
 
      var con = document.getElementById(this.cid); 
 
      con.innerHTML = con.innerHTML + text + '<br>'; 
 
     } 
 
    } 
 
    
 
    makeCommand (cmd) 
 
    { 
 
     if (cmd && cmd != '' && this.commandsList) 
 
     { 
 
      cmd = (cmd.replace('<', '&lt;')).replace('>', '&rt;'); 
 
      if (this.commandsList[cmd]) 
 
      { 
 
       console.log('Executing ' + cmd); 
 
       this.commandsList[cmd](); 
 
      } 
 
     } 
 
    } 
 
}

+0

ありがとう、それは動作します。 – Dante383

+0

ここで、 'var that = this'をもっと深く見てみましょう。http://stackoverflow.com/questions/4886632/what-does-var-that-this-mean-in-javascript – Ivan

1

あなたが別の関数内で関数を定義する場合は、残念ながらJSで、「これは」外側のスコープを指しています、ため、問題が発生し、それ代わりにグローバルオブジェクトを指しています。ブラウザで作業している場合は、 "this"が "window"オブジェクトになります。最初にconsole.logは{ foo: 'bar', log: [Function] }を印刷し、それが正しいオブジェクトを指すが、2番目の入れ子関数getThisがにconsole.logはJSでこの動作を回避するために、グローバルオブジェクトに

var object = { 
    foo: 'bar', 

    log: function() { 
     console.log("this:", this); 

     var getThis = function() { 
      console.log("this: ", this); 
     }; 

     getThis(); 
    } 
}; 

object.log(); 

を指すことになり、次の例を考えてみましょう

あなたは変数var self = thisを宣言できます。そして、私はこれは良い練習

var object = { 
    foo: 'bar', 

    log: function() { 
     console.log("this:", this); 

     var self = this; 
     var getThis = function() { 
      console.log("this: ", self); 
     }; 

     getThis(); 
    } 
}; 

object.log(); 

であると言うでしょう、あなたのコード全体でその自己を使用するか、

var object = { 
    foo: 'bar', 

    log: function() { 
     console.log("this:", this); 

     var getThis = function() { 
      console.log("this: ", this); 
     }.bind(this); 

     getThis(); 
    } 
}; 

object.log(); 
関連する問題