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
12
votes
3 answers

flow generic type for function expression (arrow functions)

I usually try to keep flow function types separate from their implementation. It's a slightly more readable when I write: type Fn = string => string; const aFn: Fn = name => `hello, ${ name }`; rather than: const aFn = (name: string): string =>…
Sam R.
  • 16,027
  • 12
  • 69
  • 122
12
votes
2 answers

Asynchronous map function that await's returns Promise instead of value

I have this code async function addFiles(dir,tree) { return (await readDir(dir)) .map(async (name) => {await readDir(dir); return name;}) } but unfortunately, it just returns a bunch of promises, because there the async function in map is not…
Zane Hitchcox
  • 936
  • 1
  • 9
  • 23
12
votes
1 answer

Why isn't this JavaScript syntax supported in Google Chrome?

I initiated a JavaScript/jQuery click listener like this: $("#test").on("click", () => { console.log("test"); }); This piece of code works perfectly fine in Firefox but in Chrome this seems to give me a Syntax error. Why is this, since this…
Biketire
  • 2,019
  • 1
  • 23
  • 41
12
votes
2 answers

When building classes in coffeescript, is there ever a reason to not use the fat arrow for the instance methods?

When building classes in coffeescript, is there ever a reason to not use the fat arrow for instance methods? Edit: Ok then! Great reply! :) To sum up, the problems are: - Takes more memory - Inability to patch - Begs question, why is it…
11
votes
3 answers

How to avoid Jest warnings: A "describe" callback must not return a value?

After upgrading Jest from version 23 to version 24, when running my tests, I get a warning message like this for almost every test: A "describe" callback must not return a value. Returning a value from "describe" will fail the test in a future…
Patrick Hund
  • 19,163
  • 11
  • 66
  • 95
11
votes
1 answer

Arrow Function Hoisting in Node?

I'm having a bit of trouble understanding why my code works. I'm expecting a reference error, but everything works fine. My code: const functionA = () => { let bResult = functionB(); console.log("Function A " + bResult); }; const functionB =…
Jason
  • 412
  • 1
  • 6
  • 13
11
votes
2 answers

Resolving promises from ES6 Arrow Functions

Reading the docs as I understand it in ES6 the meaning of: foo => someFun(foo); is equivalent to: foo => { return someFun(foo); } I'm returning a new Promise and within that code using arrow functions to invoke the resolve & reject methods,…
const
  • 111
  • 1
  • 1
  • 5
11
votes
3 answers

Is it possible to use arrow functions in classes with ES6?

My question is very simple. If I have a class in ES6 is it possible to use an arrow function within it? import React, { Component } from 'react'; export default class SearchForm extends Component { state = { searchText: '' } …
tomhughes
  • 4,597
  • 2
  • 24
  • 33
11
votes
1 answer

Classes with static arrow functions

I'm currently implementing the static land specification (an alternative of fantasy land). I want to not only use plain objects as types but also ES2015 classes with static methods. I've implemented these static methods as arrow functions in curried…
11
votes
2 answers

What is double arrow function?

What is "let x= something1 => something2 => something3" ? I have this code and I'm failing to understand what does it do. const myReducers = {person, hoursWorked}; const combineReducers = reducers => (state = {}, action) => { return…
Stav Alfi
  • 13,139
  • 23
  • 99
  • 171
11
votes
3 answers

why doesn't 'this' of an arrow function change inside an nested object literal?

I found that 'this' keyword seems always point to global in using arrow function inside a nested object literal. According to other questions, the following snippet can be explained as an arrow function's 'this' is defined in lexical context. var c…
10
votes
5 answers

ES6 functions, arrow functions and 'this' in an ES6 class

class App extends Component { constructor(props) { ... } onChange = (e) => this.setState({term: e.target.value}) onSubmit(e){ e.preventDefault(); const api_key = "C1hha1quJAQZf2JUlK"; const url =…
d_bhatnagar
  • 1,419
  • 1
  • 12
  • 20
10
votes
2 answers

Equivalent of 'this' inside lambda function

I have a function like: $(".myDropdown").hover(function() { $(this).find(".myDropdown-caretDown").hide(); }, function() { $(this).find(".myDropdown-caretDown").show(); }); If I want to use lambda function for hover callback, What can I use…
Mhd
  • 2,778
  • 5
  • 22
  • 59
10
votes
1 answer

Access to callee in arrow functions

I already know that arrow functions do not have an arguments variable bound to them. This is fine, for the most part, because I can simply use the ... operator and expand the list of arguments into an array. But arguments has a special property…
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
10
votes
4 answers

really confused by "enclosing scope" of javascript es6 arrow function

I did a lot research online, read many posts including MDN and so on. I understand that for traditional defined functions, "this" within functions is defined by objects calling/invoking them (and several different cases, object literal, new…
user3236895
  • 1,415
  • 2
  • 12
  • 13