0

What is the arrow function alternative to this function ?

function(entry) {
  //
}(entry);

I tried some things like below, but it's not the correct way :

entry => {
   //
}(entry)
Cid
  • 14,968
  • 4
  • 30
  • 45
Jerome
  • 603
  • 2
  • 5
  • 15
  • 1
    [MDN Web Docs: Arrow Functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) - Compare the two examples they provide, i.e., `function (a){ return a + 100; }` to `(a) => { return a + 100; }`. – HoldOffHunger Oct 20 '20 at 17:16
  • `(entry => {console.log(entry)})(12)` – epascarello Oct 20 '20 at 17:19
  • This *is* the correct alternative. How did you test it? Why do you think it is not correct? – VLAZ Oct 20 '20 at 17:19
  • I talk about function call... not the inside return... – Jerome Oct 20 '20 at 17:20
  • 1
    Again, your two pieces of code are *the same*. Both will work as function expressions. They cannot work when placed in a non-expression context but again, both will behave that way. – VLAZ Oct 20 '20 at 17:22
  • Typescript ask a ";" after the "}". – Jerome Oct 20 '20 at 17:23
  • 1
    A linter error is not the same as "it doesn't work" but "it is not allowed by my style enforcing tool". The code works [Playground Link](https://www.typescriptlang.org/play?#code/MYewdgzgLgBApmKAnAnjAvDArAbgFB4AUAZgK5jBQCW4hCyKAlDAN54wyiQgA2cAdDxABzOolSN8AX0ZiGkgnNQYAfK3adwEXgKGj6E6bINM8QA) - if you have any rules that prevent compilation, you should have mentioned them. You can likely change your style enforcing rule. – VLAZ Oct 20 '20 at 17:27
  • Also, for completeness, neither the [normal function](https://www.typescriptlang.org/play?#code/MYewdgzgLgBApmKAnAnjAvDArAbgFB4BmArmMFAJbgAUCyKAlDAN54wyiQgA2cAdNxABzWolQN8AX1H0JQA) nor the [arrow function](https://www.typescriptlang.org/play?#code/MYewdgzgLgBApmKAnAnjAvDArAbgFB4LJroB8MA3njDKJCADZwB0DIA5gBRGoCU+AX26I+eIA) work when not placed in expression-only context. – VLAZ Oct 20 '20 at 17:29
  • You may need to wrap the functions in parentheses. – ElectricShadow Oct 20 '20 at 17:31
  • @ElectricShadow same thing with the normal function. It will not be a valid function declaration otherwise, so it will fail as well. – VLAZ Oct 20 '20 at 17:33
  • @ElectricShadow it's OK I need to wrap the functions with parentheses. Thanks ! – Jerome Oct 21 '20 at 06:26

2 Answers2

3

Maybe you are mentioning IIFE (Immediately Invoked Function Expression)

;(function (entry) {
  console.log(entry)
})("abc")

;((entry) => {
  console.log(entry)
})("def")
hgb123
  • 13,869
  • 3
  • 20
  • 38
  • 1
    Notice the two things to learn from this answer are: 1) use parens to disambiguate what's being invoked, 2) don't invoke with the argument name, which is undefined outside the function – danh Oct 20 '20 at 17:31
0

Take a look at the two examples provided at MDN Web Docs: Arrow Functions.

Function styling...

function (a){
   return a + 100;
}

Arrow functions...

(a) => {
   return a + 100;
}

Your code should then just work with...

entry => {
   //
}

As mentioned by others, your syntax is probably based on an IIFE (Immediately Invoked Function Expression) statement.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133