ES6 arrow function with no parameters

kofifus

considering

function f() { ... }

and another function dosomething expecting a function like f

function dosomething(callback) { ...; callback() } 

that expects f (an example of dosomething can be setTimeout)

calling dosomething and passing f, is there a difference between:

dosomething(f);

and

dosomething(() => f());

is any of these options preferable ?

Quentin Roy

That the wrapping function (second example) is an arrow function or not does not change a thing here.

However, this wrapping function can be useful to forbid arguments transfer: in the first case, if callback is called with an argument, it will be given to f. Not in the the second case. An alternative could be to restrict the number of transmitted arguments: dosomething((a, b) => f(a, b));.

It can also be used to protect against this injection: in the first case, doSomething can bind f to change its this (callback.bind(whatever)). With a wrapping function (arrow or not), it won't have any effect and f will keep his this (the global context) whatever doSomething does.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ES6 if in arrow function

From Dev

ES6 Assign a variable with an arrow function

From Java

ES6 getter/setter with arrow function

From Dev

ES6 arrow / function equivalency

From Dev

Why is `this` not working in an ES6 arrow function?

From Dev

ES6 Arrow function with brackets

From Dev

ES6 Arrow Function unexpected token

From Dev

setTimeout ReactJS with arrow function es6

From Dev

ES6 Arrow Function unexpected token

From Dev

ES6 Assign a variable with an arrow function

From Dev

Es6 Arrow function to normal js

From Dev

Typescript - Arrow Function with Parameters

From Dev

Difference between this ES6 arrow function and regular function?

From Dev

How to use `this` in a function called by an ES6 arrow function?

From Dev

Immediate function using JavaScript ES6 arrow functions

From Dev

Meteor ES6 fat arrow function and `this` in onCreated not working

From Dev

Why is `throw` invalid in an ES6 arrow function?

From Dev

ES6 Arrow function is changing the scope of this in Meteor.publish

From Dev

How to change what an ES6 arrow function's 'this' points to?

From Dev

React.createClass vs. ES6 arrow function

From Dev

How do I write an arrow function in ES6 recursively?

From Dev

ES6 arrow function lexical this in V8

From Dev

jQuery .each() function with ES6 arrow functions

From Dev

ES6 fat arrow function arguments unable to comprehend

From Dev

es6 arrow function, not getting param value in React app

From Dev

default value in es6 doesn't work with arrow function

From Dev

Embed an ES6 Arrow Function in JSON Object

From Dev

es6 arrow function not recognizing this operator after transpiling to javascript

From Dev

React / ES6 - Why calling a function inside another only works with es6 arrow functions?

Related Related

  1. 1

    ES6 if in arrow function

  2. 2

    ES6 Assign a variable with an arrow function

  3. 3

    ES6 getter/setter with arrow function

  4. 4

    ES6 arrow / function equivalency

  5. 5

    Why is `this` not working in an ES6 arrow function?

  6. 6

    ES6 Arrow function with brackets

  7. 7

    ES6 Arrow Function unexpected token

  8. 8

    setTimeout ReactJS with arrow function es6

  9. 9

    ES6 Arrow Function unexpected token

  10. 10

    ES6 Assign a variable with an arrow function

  11. 11

    Es6 Arrow function to normal js

  12. 12

    Typescript - Arrow Function with Parameters

  13. 13

    Difference between this ES6 arrow function and regular function?

  14. 14

    How to use `this` in a function called by an ES6 arrow function?

  15. 15

    Immediate function using JavaScript ES6 arrow functions

  16. 16

    Meteor ES6 fat arrow function and `this` in onCreated not working

  17. 17

    Why is `throw` invalid in an ES6 arrow function?

  18. 18

    ES6 Arrow function is changing the scope of this in Meteor.publish

  19. 19

    How to change what an ES6 arrow function's 'this' points to?

  20. 20

    React.createClass vs. ES6 arrow function

  21. 21

    How do I write an arrow function in ES6 recursively?

  22. 22

    ES6 arrow function lexical this in V8

  23. 23

    jQuery .each() function with ES6 arrow functions

  24. 24

    ES6 fat arrow function arguments unable to comprehend

  25. 25

    es6 arrow function, not getting param value in React app

  26. 26

    default value in es6 doesn't work with arrow function

  27. 27

    Embed an ES6 Arrow Function in JSON Object

  28. 28

    es6 arrow function not recognizing this operator after transpiling to javascript

  29. 29

    React / ES6 - Why calling a function inside another only works with es6 arrow functions?

HotTag

Archive