это значение = неопределенное (TypeScript) - PullRequest
0 голосов
/ 19 января 2020

переменная this.engenes_comparte появляется внутри подписки как неопределенная, но снаружи, если она работает.

 baja(){    
  this._restService.getEngines(this._globalService.currentFisherMan.nid).subscribe((data : any[]) => {
    let state = 0;
        for (let index = 0; index <= data.length; index++) {
          for (let l = 0; l <= this.engines_compare.length; l++) {
            if(data[index].nid != this.engines_compare[l].nid){


            }
          }
        }
        console.log(this.engines_compare);
  });



  this.sube();
}

1 Ответ

0 голосов
/ 19 января 2020

Я не могу понять детали вашей программы, но я полагаю, что это связано с разницей в function(){ this } и () => {this}

this в разрешающей функции имеет другое значение с this в function(){} функция стиля.

let A = {
   f() {
      return this.a // this is caller of f. This `this` is not always A. 
   }
   f2 = () => {
      return this.a // This `this` always equals A
   }
   a: 1
}
A.f() // => 1 because caller of f is A, so `this` is A.
A.f.call({}) // => undefined because caller of f is not A, but {}
A.f2() // => 2 
A.f2().call({}) // => 2 because this is always A
...