【问题标题】:Can't call this inside arrow function [duplicate]不能在箭头函数中调用它[重复]
【发布时间】:2017-07-09 16:26:14
【问题描述】:

当我尝试通过this 关键字访问时遇到问题。我需要在updateTime 中访问run 函数

export class PlayerService {

 createPlayer(): void {
            return new window['YT'].Player(this.youtube.playerId, {
                height: this.youtube.playerHeight,
                width: this.youtube.playerWidth,

                events: {
                    'onStateChange': (event) => {

                        function updateTime() {

                   //////I need to access | run function | from here 

                        };

                        this.timeupdater = setInterval(updateTime, 100);


                    }
                }
            });
        }

           run (){

    //some codes 



                }
}

我正在研究 ionic 2

【问题讨论】:

  • 您能否更新您的问题以显示所有代码和失败?
  • 确保updateTime 也使用箭头函数
  • @Bergi 是的,这就是问题所在,我之前对此一无所知(问题是我必须在两者中都使用箭头),但我不明白这怎么可能是重复的问题?与您的评分问题没有任何关系:)
  • @programmer 当然是。它解释了(所有方式)如何访问像 updateTime 这样的函数中的 this 值,您将其作为回调传递给某些东西(在您的示例中为 setTimeout),以及为什么它不适用于 function .

标签: javascript angular function typescript ionic-framework


【解决方案1】:

你需要一路走箭头功能

function updateTime() {

应该是

var updateTime = () => {

setInterval(updateTime, 100);

应该是

setInterval(() => updateTime(), 100);

【讨论】:

  • 我遇到语法错误
  • 请发布准确和完整的错误。
  • 试试我的更新答案。
  • 很高兴听到你可以让它工作。已经有大约 5000 个关于同一问题的问题,因此有些人失去了耐心 ;-)
  • “和”应该是“或”
猜你喜欢
  • 2017-05-18
  • 2017-10-09
  • 2016-03-25
  • 1970-01-01
  • 2021-04-22
  • 2020-10-19
  • 1970-01-01
  • 2022-07-07
  • 2021-03-29
相关资源
最近更新 更多