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
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
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.
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.