I wanna add new key and value to this
object by destructuring assignment
, but it falls in error:
Uncaught SyntaxError: Unexpected token :
Let see my example, assume I have obj
data object:
const obj = {
'a':'1',
'b':'2',
'c':'3',
};
And now I wanna bind this data to this
object, means we wanna have:
console.log(this.a); //=> "1"
So with destructuring assignment I write like these lines:
{
a: this.a,
b: this.b,
c: this.c,
} = obj;
But it falls in error:
Uncaught SyntaxError: Unexpected token :
I don't use const
, let
or var
because the this
object has already been declared. How I can reach my desire? is it possible by destructuring assignment
?
Simply it is possible by normal assignment:
this.a = obj.a;
this.b = obj.b;
this.c = obj.c;
I just wanna right new and pretty JavaScript
codes.
You need parenthesis to distinguish the destruction object from a block statement.
({
a: this.a,
b: this.b,
c: this.c,
} = obj);