2017-03-12 10 views
-1

は、私はこの方法があります:私は地元のPlayOscillator機能番目に(クラスから)これを参照する必要typescriptのメソッドのローカル関数内で "this"を使う方法は?

export class WebAudioApiProvider { 
    private _Oscillators: OscillatorNode[] = [undefined,undefined]; 
    PlayOscillator(hz: number, durationInSeconds: number = 3){ 
     // clear the timeout (if there was one) 
     clearTimeout(this._timeOutContainer); 
     console.info("_activeOscillatorIndex: " + this._activeOscillatorIndex); 
     switch (this._activeOscillatorIndex) { 
      // There are no active oscillators 
      case -1: 
       prepareOscillators(0); 
       break; 
      case 0: 
       prepareOscillators(1, 0); 
       break; 
      case 1: 
       prepareOscillators(0, 1); 
       break; 
      default: 
       break; 
     } 
     function prepareOscillators (indexToConnect: number, indextoDispose?: number)=>{ 
      console.dir(this); 
      var success: boolean = false; 
       if (indextoDispose !== undefined && this._Oscillators[indextoDispose] !== undefined){ 
        console.dir(this._Oscillators[indextoDispose]);     
        console.log("this._Oscillators["+indextoDispose+"] is NOT undefined, it will be stoped and saved as undefined"); 
        // disconnect the current oscillator 
        this._Oscillators[indextoDispose].stop(); 
        this._Oscillators[indextoDispose] = undefined; 
       } 
     } 
    } 
} 

を、私はlet PlayOscillator = (param,param) => {}として割り当てしようとしたが、それはうまくいきませんでした。

正しい方法は何ですか?私はパラメータとして "this"を入れる必要がありますか? PlaOscillator(1, 0 , this)

+0

は '' PlayOscillator'でthis'は、問題ないはずです。あなたはそれをやっていますか? –

+0

いいえ、私はそうは思わない...私はconsole.dir "これ"私は "未定義"を得る – distante

答えて

2

function prepareOscillators =は構文エラーです。矢印機能を使用しようとしている場合は、functionを使用しないでください。const(またはlet)を使用してください。functionを使用しないでください。また、それが使われています前にそれを上に移動する必要があります:あなたはコールバックとしてこの関数を渡していない限り、

export class WebAudioApiProvider { 
    private _Oscillators: OscillatorNode[] = [undefined,undefined]; 
    PlayOscillator(hz: number, durationInSeconds: number = 3){ 
     // clear the timeout (if there was one) 
     clearTimeout(this._timeOutContainer); 
     const prepareOscillators = (indexToConnect: number, indextoDispose?: number)=>{ 
      console.dir(this); 
      var success: boolean = false; 
      if (indextoDispose !== undefined && this._Oscillators[indextoDispose] !== undefined){ 
       console.dir(this._Oscillators[indextoDispose]);     
       console.log("this._Oscillators["+indextoDispose+"] is NOT undefined, it will be stoped and saved as undefined"); 
       // disconnect the current oscillator 
       this._Oscillators[indextoDispose].stop(); 
       this._Oscillators[indextoDispose] = undefined; 
      } 
     }; 
     console.info("_activeOscillatorIndex: " + this._activeOscillatorIndex); 
     switch (this._activeOscillatorIndex) { 
      // There are no active oscillators 
      case -1: 
       prepareOscillators(0); 
       break; 
      case 0: 
       prepareOscillators(1, 0); 
       break; 
      case 1: 
       prepareOscillators(0, 1); 
       break; 
      default: 
       break; 
     } 
    } 
} 
+1

'='はタイプミスだった、私は矢印機能としてそれを入れて戻った(ctrl + z)を押してコードをここに貼り付けます。しかし、私のエラーはあなたが投稿した第2のものでした。ありがとう – distante

関連する問題