【发布时间】:2017-09-29 04:23:00
【问题描述】:
我正在编写一些打字稿代码并迭代一个数组。在循环内部,我试图访问“this”对象以进行一些处理:
console.log('before iterate, this = ' +this);
myarray.days.forEach(function(obj, index) {
console.log('before transform, this : ' + this);
this.datePipe.transform...
});
但这失败了,因为它抱怨“this”是未定义的 'this' 对象在循环之前/外部正确打印为 [object object],但在循环内部,它是未定义的。这是为什么?对此有什么解决办法?
【问题讨论】:
标签: typescript typescript2.0 typescript1.8