Javascript variable declaration with parentheses around initialized variables
1 answer
This is called a destructuring assignment. This is a new feature introduced in the ECMAScript 6 specification.
Here's an example of an object:
var test = {
"hello": 1,
"world": 2
}
If we deconstruct it like this:
var {hello, world} = test;
This is equivalent to doing:
var hello = test.hello,
world = test.world;
But, there are funnier things you can do with destructuring assignments ...
Let's assume we have this object:
var bucket = {
ExampleObject: function(input){
this.input = input.trim();
return this.input;
},
TestingObject: function(example){
this.example = example || {};
console.log(this.example.input);
}
}
Just for the record, I gave the members annoying names ... So when we destroy, we can rename them like this:
var {ExampleObject: Example, TestingObject: Test} = bucket;
The binding sample follows the syntax:
{ObjectMemberName}
// Or
{ObjectMemberName: VariableName}
For more information you can look at the draft ECMAScript 6 specification or MDN Documentation
+6
user3117575
source
to share