Questions tagged [arrow-functions]

Questions about the compact function syntax in ECMAScript2015 (ES6) called "arrow functions" because of their use of "=>".

"Arrow functions" are a compact syntax for function definitions in ECMAScript 2015 (ES6).

Arrow functions differ from regular functions in several ways:

  • They cannot be named. They are anonymous only.
  • They are not constructors, don't have a .prototype and cannot be instantiated by new.
  • They use lexically scoped this instead of binding this dynamically on the call

Arrow functions are also available in CoffeeScript.

1520 questions
8
votes
3 answers

TypeScript abstract method using lambda/arrow function

I am using TypeScript 1.6 and would like to create an abstract class with an abstract method but use a lambda/arrow function in the concrete class. Is this possible? The code shown below does not compile as it says "Class 'Base' defines instance…
8
votes
1 answer

JavaScript ecma6 change normal function to arrow function

I have that code: function defineProperty(object, name, callback){ if(object.prototype){ Object.defineProperty(object.prototype, name, {"get": callback}); } } defineProperty(String, "isEmpty", function(){return this.length ===…
s77s
  • 304
  • 1
  • 2
  • 11
7
votes
3 answers

Coffeescript classes and scope and fat and thin arrows

In a fat arrowed function of a coffeescript class, how can I access the scope of the class as well as the function? Example: class Example foo: -> $('.element').each => # or -> @bar($(this)) # I want to access 'bar' as well as…
Jo P
  • 1,656
  • 21
  • 25
7
votes
2 answers

webpack + babel - transpile object arrow functions doesn't work

I'm trying to configure webpack (5) with babel, using babel-loader to transpile to ES5. Unfortunately, the output is not consistent. Basically, it's divided into two parts: Some polyfills: My code: As you can see, the first part contains arrow…
RonZ
  • 743
  • 1
  • 11
  • 31
7
votes
4 answers

React Arrow Function Component - setState is not defined

I am trying to setState in an arrow function component but getting an error "setState is not defined". I tried setting state in handleChange using setState({ selectedSlot }) and this.setState({ selectedSlot }) but nothing worked. const…
Raghav
  • 343
  • 1
  • 4
  • 8
7
votes
6 answers

The this keyword not working with arrow functions

I'm learning ES6, I just want to convert my ES5 knowledge to ES6. here's my ES5 code: function click() { this.className += ' grab'; setTimeout(() => (this.className = 'remove'), 0); }; and here's my ES6 code: const click = () => { …
Japs
  • 977
  • 2
  • 10
  • 19
7
votes
2 answers

Classes and arrow functions and THIS

While learning JS and React I've come across confusing differences in tutorials. I'll split the questions below with examples. I understand bind for regular functions and this context, it's just arrow functions and how I've seen them used/declared…
j obe
  • 1,759
  • 4
  • 15
  • 23
7
votes
1 answer

Syntax error for overloading generic arrow function in Typescript

interface Foo1 { (param: number): number (param: string): string } const foo1: Foo1 = (param: number | string) => param as any const foo2 = (param: T) : T => param interface Foo3 { (param: T1): T1 (param: T2): T2 } const foo3:…
Joshua
  • 5,901
  • 2
  • 32
  • 52
7
votes
2 answers

React onChange functions with ES6 arrows or not

When handling an onChange event, this one works:
GWorking
  • 4,011
  • 10
  • 49
  • 90
7
votes
2 answers

typescript overload method with arrow function

anyone know how to use method overload on arrow function? foo(args: string): string foo(args: number): number foo(args: string | number): string | number { if (typeof args === "string") { return "string" } …
Jemmy Phan
  • 73
  • 2
  • 5
7
votes
2 answers

Should one use const when declaring an arrow function in React class

Inside a react class component, should one use a const/let to declare an arrow function, or they should be emmited: class ReactComp extend Component { const sayHello = () => { return 'Hello'; } sayBye = () => { …
user9226007
7
votes
3 answers

How to translate 'this' in D3 JavaScript to TypeScript?

I know that 'this' in JavaScript has a different meaning than in in TypeScript, as per this article 'this' in TypeScript. I have the following code in JavaScript used to create a thicker stroke on the selected node, and give all other nodes a…
7
votes
2 answers

How to test arrow function in React ES6 class

I used arrow function inside of my React component to avoid binding this context, for example my component look like this; class Comp extends Component { _fn1 = () => {} _fn2 = () => {} render() { return (
); } } How do…
Leo Hsieh
  • 71
  • 1
  • 2
7
votes
2 answers

How to run ES6 code with arrow functions in Safari?

For some reason, ES6 code that runs well in the current Chrome or Firefox cannot run in Safari - for example, arrow functions. As I know, Safari has ok support for ES6. Is there something that needs to be done? Example: var arr = [1,3,5].map((i)…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
7
votes
2 answers

Using arrow functions in TypeScript: how to make them class methods?

I'm fairly experienced programming but quite new to TypeScript. Trying to use it with jQuery and immediately ran into the 'this' issue with callbacks (such as $(document).ready. Using $.proxy() is one way to go, but using TypeScript's arrow…
Robert
  • 71
  • 1
  • 3