" 真的很好用!!!" /> " 真的很好用!!! - 秒客网" />

TypeScript 中的 "=>" 真的很好用!!!

时间:2023-12-24 11:58:43
class funs
{ public $scope: IBarPadScope; constructor($scope: IBarPadScope) { this.$scope = $scope; }
public createLoadPhoneOrders(startTime?: string): () => void {
return () => {
startTime = startTime || moment().format("YYYY-MM-DD 00:00:00");
this.$scope.socket.emit('command',
{
command: 'query',
data: {
Node: this.$scope.sys.node,
CreatedTime: { '$gte': startTime }
},
document:'PhoneOrders'
},
(err, r: PhoneOrder[]) => {
this.$scope.$apply(() =>
{
this.$scope.allPhoneOrders = r;
});
});
};
}
}

可以看到多级子函数调用 this一直指向的是funs的实例

看看编译出来的js的代码

funs.prototype.createLoadPhoneOrders = function (startTime) {
var _this = this;
return function () {
startTime = startTime || moment().format("YYYY-MM-DD 00:00:00");
_this.$scope.socket.emit('command', {
command: 'query',
data: {
Node: _this.$scope.sys.node,
CreatedTime: { '$gte': startTime }
},
document: 'PhoneOrders'
}, function (err, r) {
_this.$scope.$apply(function () {
_this.$scope.allPhoneOrders = r;
});
});
};
};

相关文章