如何在JavaScript中将我的粗箭头功能转换为“正常”功能

时间:2019-07-01 20:23:56

标签: 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
    );

1 个答案:

答案 0 :(得分:0)

// 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
    );
});

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