[英]this value = undefined ( TypeScript)
变量 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();
}
我无法理解你的程序的细节,但我想这是由于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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.