Typescript-带参数的箭头函数
我对Typescript还不熟悉,并试图将它介绍给我的一些东西,但我在一些作用域和箭头函数方面遇到了困难 在javascript中,我的代码如下所示Typescript-带参数的箭头函数,typescript,arrow-functions,Typescript,Arrow Functions,我对Typescript还不熟悉,并试图将它介绍给我的一些东西,但我在一些作用域和箭头函数方面遇到了困难 在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 opera
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
});
好的,这有两个问题。当我在Typescript中这样做时,我是这样做的
class ClassName {
model: any;
subscribe() {
this.model.Prototypes.bind("change", function(e){
// FIRST PROBLEM
this.model ....
});
}
}
好的,这一直到标签部分this.model
不再是我所认为的引用,因为它是在函数的上下文中,而不是在“类”中。因此,我做了一些挖掘,了解到我应该使用箭头函数
,因为这样可以保留上下文
问题是,我无法想象如何执行箭头函数并仍然传递所需的参数,如绑定事件的
change
值,或函数(e)
部分。我只看到了不需要任何参数的示例。arrow/lambda语法如下所示:
class ClassName {
model: any;
subscribe() {
this.model.Prototypes.bind("change", e => {
// FIRST PROBLEM
this.model ....
});
}
}
如果有多个参数,请使用以下格式:
(p1, p2, p3) => { ... }
希望这有帮助