Alex Cory
Alex Cory

Reputation: 11845

Can you create an object when destructuring function arguments?

Basically I'm trying to see if I can do this:

var state = {
  lastMessage: {
    owner: 'lastMessageOwnerID'
  },
  owner: 'currentMessageOwnerID'
}

function isSameMessageOwner ({ lastMessage, owner: currentMessage: { owner } }) {
  return lastMessage.owner === currentMessage.owner
}

var isSameOwner = isSameMessageOwner(state)

I don't think you can, but just checking :)

Upvotes: 0

Views: 71

Answers (2)

Felix Kling
Felix Kling

Reputation: 816312

Can you create an object when destructuring function arguments?

No. Destructuring only allows you to assign existing values to variables. It doesn't allow you to create new values.

Upvotes: 1

synthet1c
synthet1c

Reputation: 6282

Do you mean this? I'm on the fence about destructuring deeply nested objects, I think it's hard to read, but maybe it will get better with time.

var state = {
  lastMessage: {
    owner: 'lastMessageOwnerID'
  },
  owner: 'currentMessageOwnerID'
}

function isSameMessageOwner ({
  lastMessage: { owner: lastMessage },
  owner: currentMessage
}) {
  return lastMessage === currentMessage
}

var isSameOwner = isSameMessageOwner(state)

console.log(isSameOwner)

Upvotes: 0

Related Questions