首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套方法中的Hit方法

嵌套方法中的Hit方法
EN

Stack Overflow用户
提问于 2017-10-17 04:34:25
回答 2查看 39关注 0票数 0

如何在类型记录中从单独的内嵌函数中命中函数?

我想要的是accomplish...in伪;

代码语言:javascript
复制
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或其他东西。那我错过了什么?谢谢你的指导,这让我困扰了很久,我不愿意分享:)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-17 06:30:56

this会疯的,我知道,但是试试that ;)

代码语言:javascript
复制
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,您可以得到所需的函数;)这都是关于变量作用域的

票数 0
EN

Stack Overflow用户

发布于 2017-10-17 04:44:04

所以我错过了什么

错误的this

修复

使用箭头函数

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46782531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档