0
const barker = (state) => {
    bark: () => console.log('Woof, I am ' + state.name)
}

const barker2 = (state) => ({
    bark: () => console.log('Woof, I am ' + state.name)
})

barker===barker2 //returns false

My goal is to use Object.assign within a factory function and use 'barker' as an input. 'barker' doesn't work but 'barker2' works. Why does it work with the set of parenthesis? I understand IIFE's but this isn't immediately called so I don't understand what's happening here.

  • Because the second is an arrow function returning an object, the first isn’t. Not sure how IIFEs are involved; can you explain? – Dave Newton Jul 15 '22 at 01:33
  • Rare bit of JS syntax: in your first function `bark:` is [a label](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label), which is why it "seems to work" (even if it doesn't do what you thought it'd do) – Mike 'Pomax' Kamermans Jul 15 '22 at 01:39
  • @DaveNewton The look reminded me of IIFE's (function wrapped in parenthesis then called with another set) so I thought maybe there was some similarity or concept I was missing. I think I understand where I went wrong though. – caveman418 Jul 15 '22 at 01:44
  • `barker===barker2` will never return `true` since they're two distinct function objects. – Bergi Jul 15 '22 at 01:48

0 Answers0