javascript - 如何将我的胖箭头函数转换为 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
);
解决方案
// 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
);
});
我希望这会有所帮助,如果您有任何问题,请告诉我:)
推荐阅读
- python - Python优化字典迭代
- apache-kafka - Kafka-streams 破坏了消息处理语义
- java - 如何在 Freemarker 模板中创建带有文本的表格?
- python - 为什么更改 numpy 子类的属性不会更改子类数组?
- c# - 如何将 ObervableCollection 或 IEnumerable 绑定到流文档表
- xamarin.android - 在不使用线性布局的情况下动态设置复选框的边距
- mysql - 将 Wordpress 站点移动到 Docker:建立数据库连接时出错
- java - Android 应用程序似乎在 KitKat 4.4 上启动时崩溃
- java - why am I receiving stale element reference on test method?
- angular - 找不到模块“./sc-modal/sc-modal.module”