简体   繁体   English

此值 = 未定义(打字稿)

[英]this value = undefined ( TypeScript)

the variable this.engenes_comparte is appearing inside the subcribe as undefined but outside if it works.变量 this.engenes_comparte 出现在 subcribe 内为 undefined 但如果它有效,则出现在外面。

 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();
}

I can't understand detail of your program, but I guess it is due to difference of this in function(){ this } and () => {this}我无法理解你的程序的细节,但我想这是由于function(){ this }() => {this}

this in allow function has different meaning with this in function(){} style function. this在允许功能有不同的含义thisfunction(){}风格的函数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM