我对打字稿很陌生,并试图把它介绍给我的一些东西,但我在某些范围和箭头功能上有困难。
在javascript中,我的代码如下..。
var model = params.model;
model.Prototypes.bind('change', function(e){
// some code to execute when the event occurs
model.set([some values]); // this performs an operation to the actual top level model
});好吧,这有两个问题。当我在打字稿上做这件事时,我是这样做的.
class ClassName {
model: any;
subscribe() {
this.model.Prototypes.bind("change", function(e){
// FIRST PROBLEM
this.model ....
});
}
}好的,这个一直工作到标签上。this.model不再是我所认为的引用,因为它是在函数的上下文中,而不是‘类’。因此,我做了一些调查,并了解到我应该使用一个arrow function,因为这将保留上下文。
问题是,我无法想象如何执行箭头函数,并且仍然传递我需要的参数,比如绑定事件的change值或function(e)部件。我只看到了一些完全没有参数的例子。
发布于 2014-01-17 21:56:20
箭头/lambda语法如下所示:
class ClassName {
model: any;
subscribe() {
this.model.Prototypes.bind("change", e => {
// FIRST PROBLEM
this.model ....
});
}
}如果有多个参数,请使用以下格式:
(p1, p2, p3) => { ... }希望这能帮上忙
https://stackoverflow.com/questions/21196609
复制相似问题