首页 > 解决方案 > 如何将我的胖箭头函数转换为 javascript 中的“正常”函数

问题描述

我只是想将其转换为没有“胖箭头”的普通 js 函数。

我希望 body => 成为一个正常的功能。

我该怎么写?

代码片段

fetch(this.JobExecEndpoint)
  .then(response => response.json())
  .then(body => {
    this.cleanStartTime = moment(body[0].time_start);
    this.cleanEndTime = moment(body[0].time_end);
    this.cleanDuration = this.calculateDuration(
      this.cleanStartTime,
      this.cleanEndTime
    );

标签: javascript

解决方案


// Hold a reference to "this" object inside a variable else you won't be able to access it using the "this" inside the callback function

var that = this; 

fetch(this.JobExecEndpoint).then(function(response)
{
     return response.json();
})
.then(function(body)
{
    that.cleanStartTime = moment(body[0].time_start);
    that.cleanEndTime   = moment(body[0].time_end);
    that.cleanDuration  = that.calculateDuration (
        that.cleanStartTime,
        that.cleanEndTime
    );
});

我希望这会有所帮助,如果您有任何问题,请告诉我:)


推荐阅读