Senasiko
Senasiko

Reputation: 109

ES6 function destructuring assignment as a object

First, this is a function accept a param what typeof Object, just like

function(object) {}

In ES6+, we can write like

function({ key }) {}

And now I want use the object, just like

function(object:{ key }) {
  console.log(key);
  console.log(object)
}

Upvotes: 0

Views: 101

Answers (1)

Estus Flask
Estus Flask

Reputation: 222334

Once a parameter is destructured, it isn't available (except for arguments in regular functions). There is no such syntax as function(object:{ key }) {...}.

If original object is supposed to be used, it shouldn't be destructured as a parameter:

function(object) {
  const { key } = object;
  console.log(key);
  console.log(object)
}

If there's no real benefit in using key, desctructuring can be skipped in favour of object.key.

Upvotes: 2

Related Questions