Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript-带参数的箭头函数_Typescript_Arrow Functions - Fatal编程技术网

Typescript-带参数的箭头函数

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

我对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 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) => { ... }
希望这有帮助