Skip to content
Advertisement

What does it mean by Object.assign(fn, {x: () => {}, y: () => {}})?

Let’s I have a function defined as below,

JavaScript

When I do console.log(calculateHash()), it logs as,

JavaScript

…and when I do,

JavaScript

It actually calls the async function.

JavaScript

How does it work here?

When I do Object.assign(obj1, obj2), it will merge them; if properties are same it will override.

So, I think here Object.assign() creates a final object as below

JavaScript

But on calling calculateHash(), how does it know to call the first property?

Advertisement

Answer

Functions are objects, and Object.assign mutates it’s first argument and returns it:

JavaScript

If you do

JavaScript

Then add3 is still a function. You’re being led astray by what the console is showing you, that async anonymous function isn’t a property of the resulting object, it is the resulting object, with some stuff added to it. Note that “property” is in square brackets, and there’s no name associated with it.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement