[英]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.