ES6 anonymous functions

Starting to learn ES6, already know ES5 --> https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-arrow-functions-to-write-concise-anonymous-functions

var magic = function() {
  "use strict";
  return new Date();
};

Is translated too

const magic = () => new Date()

I am trying to understand some uses of this function.

Would i call it with () or no? (magic() || magic)

whatIsTheDate = magic()
// could be the same as
whatIsTheDate = new Date()

Is translated too
const magic = () => new Date()

Basically, yes.

Would i call it with () or no? (magic() || magic)

With magic(). You are creating a variable called magic that is referencing an anonymous function you define after it. You can refer to that anonymous function with magic (if you want to assign another variable to it, or pass it as a parameter) or as magic() if you want to invoke (run) the function.

whatIsTheDate = magic
// could be the same as
whatIsTheDate = new Date()

No, because the first one just assigns the function into that variable (whatIsTheDate) but doesn’t actually run it. You just have a new pointer to that anonymous function.

You could say:

whatIsTheDate = magic()
// is functionally equivalent to
whatIsTheDate = new Date()

My point. Does one line functions actually have a purpose? I can only think of one examples.

I’m not sure I understand your question. If I do understand, you are asking, why bother with the function magic() since it can be just replaced with one line. What is the benefit?

I can think of a few:

  1. Hiding details - if the function does some complex manipulation (not the case here) it may be nice to hide it away and have a nice, easy to understand function name. I often do this for rendering complex strings, especially with html, I hide it in a function then call let titleStr = renderTitle(title, author).

  2. DRY - If that code is called in a lot of places, it is much better to have it in one place. That way if something needs to be changed, it can be changed in one place and you don’t have to go searching through all the code, finding everywhere you do that, and hope you don’t make a typo on one. This is a very important concept in maintainable code.

Then you have to scroll around the code looking for it.

No point in making a 1 line function for something as simple as new Date(). We all know what it means, just like i don’t need a separate function to split a string. Its already a keyword.

Something as big as:

FBPosts.filter((post) => post.thumbnail !== null && post.shares > 100 && post.likes > 500)

Would do better.

I feel like the challenge was to introduce you to them, and they can be more useful for bigger stuff like above.

Then you have to scroll around the code looking for it.

Only if you’ve named it poorly. If you name things well, then you just use them where you need them.

No point in making a 1 line function for something as simple as new Date().

Well that was kind of what I was getting at with “(not the case here)”. But it’s still not open and shut. What if you implemented that and used it in 127 different places in 36 different files and then your manager said, “OK, but now I want the date/time rounded down to the nearest hour.” Which would you rather do? Go and change it in one place in a file of helper functions? Or try and find every occurrence and hope you don’t make a typo?

That’s and extreme case, but it makes the point. These conventions exist for a reason. Coders didn’t invent these to make life harder for themselves. Especially if there is something that requires extra work, there is probably a very good reason.

Yes, in a small program you can get away with that. But do you want to do small programs forever? We should always be using best practices so when we need them, they just flow naturally.

Of course, we can go overboard, turning everything into a function. For me, this is kind of borderline and how I would handle it would depend on some other factors. How big is the code? How many places do I need this? Is it likely to change?

1 Like