Hacker News new | ask | show | jobs
by jknutson 613 days ago
3 ways to declare functions? I am probably blanking but I can only think of:

``` function foo () {} const foo = () => {} ```

5 comments

    function x() {/* ... */}
    const x = function() {/* ... */}
    const x = function foo() {/* ... */}
    const x = (function() {/* ... */}).bind(this)
    const x = (function foo() {/* ... */}).bind(this)
    const x = () => {/* ... */}
    const x = () => /* ... */
Apart from hoisting (which has little to do with functions directly) and `this` these are all equivalent
Not sure if it counts but there is `new Function("return x;")`
Doesn't `function* ()` count?

After all, you can add a `*` to any existing function without a change in the function or its callers.

4 ways
5 ways, the arrow functions have two different syntaxes:

  () => { return 1; }
  () => 1
That’s still one way - arrow function.
Well, 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.

`const foo = function() {}`
Do function expressions count?
const x = { foo() {} }