1

I am confused by this function:

const today = ( d => new Date() )(new Date);

So I am not sure where to read about this and I am not sure what this is even called. So I apologize for the terrible question title. Anyways, I am confused as to what the (new Date) is doing at the end of the function here. Can someone point me to a reference to what is happening here or explain it to me.

I understand the new Date() mumbo jumbo. It is really just the purpose and functionality of (new Date) at the end that is tripping me up.

Thomas Valadez
  • 1,697
  • 2
  • 22
  • 27

4 Answers4

2

Here you have a simple function which accepts an parameter with name d and returns a new Date

const f = d => new Date();

console.log(f());

Then you call this function immediately with () passing a new Date object, which is unused in the function

This code is equivalent

const today = (d => new Date())(new Date)

to this

const func = d => new Date();
const today = func(new Date);

In the first part with one line the difference is that you have lost the the function which you have created, because it was not assigned to anything. You have created and called it immediately.

Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
  • Ok. That is super obvious. Thanks. I just wasn't seeing it. – Thomas Valadez Feb 27 '18 at 04:20
  • 1
    Here what is the intention of passing the new Date() as an argument If it is unused? – Vineesh Feb 27 '18 at 04:27
  • @Vineesh what's the purpose of creating a function in the first place when you could just do `const today = new Date()` ? My guess is there is more that is going on and this is only a building block in some book or tutorial. – vol7ron Feb 27 '18 at 04:28
  • Now i can turn it into something like this `const yesterday = ( d => new Date(d.setDate(d.getDate()-1)) )(new Date);` – Thomas Valadez Feb 27 '18 at 04:30
  • Developers don't write the way they do out of purpose, they write code the way they do out of habit. At least that's my observation of non-senior dev coworkers. @Thomas Valadez - Or you could save everyone the trouble of reading that nonsense and use a semantic library like momentjs – aaaaaa Feb 27 '18 at 04:31
  • 1
    thanks @aaaaaa for your comment. I am just trying to understand the syntax of ES6 and how to work with it. That way maybe someday I can contribute to a library like momentjs. – Thomas Valadez Feb 27 '18 at 04:37
1

You are invoking the function immediately after creating it like IIFE, so the result variable will contain the result you passed. You are doing the same thing with the arrow function.

In the below example both expressions are the same. It’s invoking the function immediately:

const today = d => console.log(d);
today(new Date)

const today = ( d => console.log(d) )(new Date());

// In ES5 normal IIFE

(function(d){
  console.log(d);
})(new Date())
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
zabusa
  • 2,520
  • 21
  • 25
1
const today = ( d => new Date() )(new Date);
▙▂▂▂▂▂▂▂▂▟   ▙▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▟ ▙▂▂▂▂▂▂▂▟
    ▐                  ▐               ▐
 variable     function definition    function invocation/call
  • today will hold the value returned by the anonymous function
  • the anonymous function will dissipate into the ether
  • first the anonymous function will take a pointless new Date argument, completely disregard it and return a new Date() value (to be assigned to today)
vol7ron
  • 40,809
  • 21
  • 119
  • 172
1

This looks more confusing that it actually is. If we broke it down:

// The following function returns a new Date
// regardless of any arguments passed to it

const someFunction = d => newDate();

// This could also be written as
const theSameFunction = () => newDate();

An IIFE (immediately invoked function expression) is one that is run immediately upon definition. This can be accomplished by wrapping an anonymous function in parenthesis, and calling it:

( () => console.log('Hello world') )();

// prints 'Hello world' to the console

Putting all that together:

const today = ( d => new Date() )(new Date);
// The d argument here is useless,
// since it is never used in the return of the IIFE

const alsoToday = ( () => new Date() )();
// This achieves the same effect as the first IFFE
stukilgore
  • 101
  • 3