Typescript - Arrow Function with Parameters

Ciel

I am new to Typescript, and attempting to introduce it to some of my stuff, but I am having difficulty with some scope and arrow functions.

In javascript, my code looks like this ...

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
});

Alright, so this has two problems. When I go to do this in Typescript, I do it like this...

class ClassName {
   model: any;

   subscribe() {
      this.model.Prototypes.bind("change", function(e){
         // FIRST PROBLEM
         this.model ....
      });
   }
}

Alright, so this works up until the labelled part. this.model is no longer a reference to what I think it is, because it is in the context of the function, not the 'class'. So I did some digging and learned I should be using an arrow function, because that will preserve the context.

The problem is, I cannot conceive of how to do an arrow function and still pass through the parameters I need, like the change value for the bind event, or the function(e) part. I have only seen examples that expect no parameters at all.

x0n

The arrow/lambda syntax would look like this:

class ClassName {
   model: any;

   subscribe() {
      this.model.Prototypes.bind("change", e => {
         // FIRST PROBLEM
         this.model ....
      });
   }
}

If you have more than one parameter, use this format:

(p1, p2, p3) => { ... }

Hope this helps,

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Typescript: this inside parameters of arrow function

From Dev

Typescript Decorators and Arrow Function

From Dev

Strange arrow function parameters behavior

From Dev

Type for parameter in TypeScript Arrow Function

From Dev

ES6 arrow function with no parameters

From Dev

typescript arrow function parameter type safe

From Java

Specify return type in TypeScript arrow function

From Dev

TypeScript abstract method using lambda/arrow function

From Dev

typescript arrow operator to define a function on prototype

From Dev

Pass typescript arrow function in text (Angular)

From Java

Convert interface into Function parameters in Typescript

From Dev

Typescript inherited classes as function parameters

From Dev

Typescript 1.4: Arrow function without curly braces, syntax changed?

From Dev

Typescript: Arrow function - TS2339: Property does not exist on type '{}'

From Dev

React with Typescript: Types for array.map() with arrow function

From Dev

Typescript: why function without parameters can be cast to function with parameters

From Java

Typed function parameters using destructuring and rest in TypeScript

From Dev

Typescript - Pass interface literals to function parameters

From Dev

TypeScript call function with Rest Parameters from another with Rest Parameters

From Dev

Typescript overload arrow functions

From Dev

Typescript: rewrite a fat arrow function a do not lose bare function signature check

From Dev

Arrow function is not a function

From Dev

Differences in arrow function and no arrow function in ReactJS?

From Dev

Can't console log in short hand arrow function when using Typescript

From Dev

typescript variable number of parameters in function interface definition 0.9.5

From Dev

How can I add optional named parameters to a TypeScript function parameter?

From Dev

TypeScript - typing rest parameters and return values when wrapping a function

From Dev

How to use MobX State Tree models as function parameters with TypeScript?

From Dev

How can I add optional named parameters to a TypeScript function parameter?

Related Related

  1. 1

    Typescript: this inside parameters of arrow function

  2. 2

    Typescript Decorators and Arrow Function

  3. 3

    Strange arrow function parameters behavior

  4. 4

    Type for parameter in TypeScript Arrow Function

  5. 5

    ES6 arrow function with no parameters

  6. 6

    typescript arrow function parameter type safe

  7. 7

    Specify return type in TypeScript arrow function

  8. 8

    TypeScript abstract method using lambda/arrow function

  9. 9

    typescript arrow operator to define a function on prototype

  10. 10

    Pass typescript arrow function in text (Angular)

  11. 11

    Convert interface into Function parameters in Typescript

  12. 12

    Typescript inherited classes as function parameters

  13. 13

    Typescript 1.4: Arrow function without curly braces, syntax changed?

  14. 14

    Typescript: Arrow function - TS2339: Property does not exist on type '{}'

  15. 15

    React with Typescript: Types for array.map() with arrow function

  16. 16

    Typescript: why function without parameters can be cast to function with parameters

  17. 17

    Typed function parameters using destructuring and rest in TypeScript

  18. 18

    Typescript - Pass interface literals to function parameters

  19. 19

    TypeScript call function with Rest Parameters from another with Rest Parameters

  20. 20

    Typescript overload arrow functions

  21. 21

    Typescript: rewrite a fat arrow function a do not lose bare function signature check

  22. 22

    Arrow function is not a function

  23. 23

    Differences in arrow function and no arrow function in ReactJS?

  24. 24

    Can't console log in short hand arrow function when using Typescript

  25. 25

    typescript variable number of parameters in function interface definition 0.9.5

  26. 26

    How can I add optional named parameters to a TypeScript function parameter?

  27. 27

    TypeScript - typing rest parameters and return values when wrapping a function

  28. 28

    How to use MobX State Tree models as function parameters with TypeScript?

  29. 29

    How can I add optional named parameters to a TypeScript function parameter?

HotTag

Archive