如何在类型记录中从单独的内嵌函数中命中函数?
我想要的是accomplish...in伪;
export class blah {
functionOne(result) {
// do stuff with the result of functionTwoChildMethod...
};
const geocoder = new google.maps.Geocoder();
geocoder.geocode (paramObj, function (res, stat) {
functionTwoChildMethod = () => {
this.functionOne(result);
};
};
};执行this.functionOne(result);似乎没有到达类的作用域,以使父类触发functionOne或其他东西。那我错过了什么?谢谢你的指导,这让我困扰了很久,我不愿意分享:)
发布于 2017-10-17 06:30:56
this会疯的,我知道,但是试试that ;)
export class blah {
functionOne(result) {
// do stuff with the result of functionTwoChildMethod...
};
const geocoder = new google.maps.Geocoder();
var that = this;
geocoder.geocode (paramObj, function (res, stat) {
functionTwoChildMethod = () => {
that.functionOne(result);
};
};
};您正在使用的this在一个作用域中,其中is没有您需要的函数,但是通过将它分配给that,您可以得到所需的函数;)这都是关于变量作用域的
发布于 2017-10-17 04:44:04
所以我错过了什么
错误的this。
修复
使用箭头函数
export class blah {
functionOne(result) {
// do stuff with the result of functionTwoChildMethod...
};
const geocoder = new google.maps.Geocoder();
geocoder.geocode (paramObj, (res, stat) => { // Use arrow here
functionTwoChildMethod = () => {
this.functionOne(result);
};
};
};更多
https://basarat.gitbooks.io/typescript/docs/arrow-functions.html
https://stackoverflow.com/questions/46782531
复制相似问题