6 ms·
3 ways to declare functions? I am probably blanking but I can only think of: ``` function foo () {} const foo = () => {} ```
by jknutson 2y ago
3 ways to declare functions? I am probably blanking but I can only think of:
```
function foo () {}
const foo = () => {}
```
- taosx 2y agoNot sure if it counts but there is `new Function("return x;")`
- wvenable 2y ago4 ways
- Izkata 2y ago5 ways, the arrow functions have two different syntaxes: () => { return 1; } () => 1
- wiseowise 2y agoThat’s still one way - arrow function.
- Izkata 2y agoWell, these have the same result, so if the two types of arrow functions don't count as different then neither should these two assignment versions: const foo = (function() {}).bind(this); const foo = () => {}; Edit: And speaking of assignment versions, there's a new comment that adds a third of the same. I kinda get the feeling a lot of the "multiple ways to declare functions" is just people who don't understand how the pieces of javascript fit together and think these are all independent. They're not. Just declaring a function has only a few ways, but declaring a function and giving it a name multiplies that out to some extent. In javascript, functions are first-class objects: they can be assigned to variables and passed around just like numbers or strings. That's what everything except "function foo() {}" is doing.
- lelanthran 2y agoDoesn't `function* ()` count? After all, you can add a `*` to any existing function without a change in the function or its callers.
- onsclom 2y ago`const foo = function() {}`
- mr_toad 2y agoDo function expressions count?
- kevlened 2y agoconst x = { foo() {} }
- renlo 2y agofunction x() {/* ... */} const x = function() {/* ... */} const x = function foo() {/* ... */} const x = (function() {/* ... */}).bind(this) const x = (function foo() {/* ... */}).bind(this) const x = () => {/* ... */} const x = () => /* ... */
- afiori 2y agoApart from hoisting (which has little to do with functions directly) and `this` these are all equivalent