0

I'm just starting to learn JS. I need to redo this function into an explicit one. As a result, the property should be added to the object. And please explain how it works.

const newPost = (post, addedAt = Date()) => ({
    ...post,
    addedAt,
})

const firstPost = {
    id: 1,
    name: 'Max'
}

console.dir(newPost(firstPost))

That's all I could think of. There are no further ideas

const firstPost = {
    id: 1,
    name: 'Bogdan'
}

const newPost = (addedAt = Date(), ...post) => {

    return post
}

newPost(firstPost)
  • 4
    What are you trying to do exactly? – Unmitigated Feb 04 '23 at 20:58
  • What do you mean by "explicit function"? – Bergi Feb 04 '23 at 21:28
  • Your first code snippet seems to run fine. What's the problem? – Bergi Feb 04 '23 at 21:29
  • Welcome to Stack Overflow! Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output, preferably in a [Stacksnippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) – mplungjan Feb 04 '23 at 21:33
  • I need to pass this function with the construction function {}. Without parentheses – The Vortex Feb 04 '23 at 21:40
  • not clear what you mean, or why you'd need/want to avoid parenthesis. But see https://stackoverflow.com/q/28770415/1048572 for syntax alternatives - or just use `function`. – Bergi Feb 04 '23 at 22:55
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 05 '23 at 05:37

0 Answers0