Reputation: 2998
I expected this to result in 'woof,' but I'm getting undefined. Clearly not doing something correctly. Can someone tell me what I'm doing wrong here?
const barker = (state) => {
return {
speak: () => console.log(state.sound)
}
}
const newAnimal = sound => {
let state = {
sound
}
console.log(state.sound)
return Object.assign({}, barker(state))
}
console.log(newAnimal("woof").speak())
Upvotes: -1
Views: 45
Reputation: 155708
Here's your problem:
speak: () => console.log( state.sound )
barker
returns an Object with a function-property speak
which does not return a value (aka undefined
). It has the type () => void
because console.log
does not return a value.
You can change it to this:
speak: () => {
console.log( state.sound );
return state.sound;
}
Or even just this (without logging):
speak: () => state.sound;
Upvotes: 1
Reputation: 620
Your code works perfectly. It outputs 'woof' 2 times (once from console.log inside newAnimal and once inside speak()). You get undefined because you also output the result of the speak method which is the return value of console.log, which is always undefined.
Upvotes: 1