2

I just faced this Java Script code:

const {
  myKey,
  uname,
  issorted,
  title,
  hClick,
} = this.props;

Please tell me what this means? What are its implications?

Thanks

3 Answers 3

5

This is an example of Destructuring Assignment. Basically it's declaring variables from each of the provided object keys. You'll be able to access those variables like

console.log(uname)

as opposed to

console.log(this.props.uname)

In addition to this, if you're asking about 'const' as well, this has a good explanation

Sign up to request clarification or add additional context in comments.

1 Comment

Worth noting that deconstruction can be done to array arguments, it does not only occur in the example provided in the question
1

In recent versions of Javascript, destructuring has become a popular method of accessing certain values from within objects, and localizing them.

In this case, this.props contains properties myKey, uname, issorted, title, hClick, possibly among others. In order to obtain on specific properties instead of copying the entire object, we do destructure assignment and localize each property according to their name, as shown by the code you've provided.

Comments

1

It's an object destructuring assignment. In your code example it extracts data from this.props object into distinct constants declared on the left-hand side of the assignment (myKey, uname, issorted ...). So if this.props was an object like this:

this.props =  {
    myKey: 'value1',
    uname: 'value2',
    issorted: 'value3',
    title: 'value4',
    hClick: 'value5'
}

after calling mentioned code the declared constants (myKey, uname, issorted ...) would have values of corresponding object properties - e.g. myKey constant would be equal to value1.

In your code sample const tells that object data will be extracted into constants but of course you can also use variables declared with let or var.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.