I'm calling an function which is returning object with two properties. I want to save these property values to global variables(LWC properties) using Object de-structuring but I'm not able to do that. Below is the function:


   let gameParams={}
   if(mode == 'easy'){
     gameParams.count = 3;
     gameParams.moves = 5;
   }else {
     gameParams.count = 6;
     gameParams.moves = 9;
   return gameParams;

On return when trying to save the object values into global variables using Object de-structuring, getting syntax error: Unexpected token or Declaration or statement expected:

 {this.count, this.moves} = getParameters(mode);

I also tried below approach by enclosing the entire statement in parenthesis but it also didn't work.

 ({this.count, this.moves} = getParameters(mode));

When I try to save the properties into local variables it works but not with the global variables (accessed using this). Below line works

 const {newCount, newMoves } = getParameters(mode);

can someone advise how to save these property values in global variables? Full code: Demo

2 Answers 2


I was able to resolve with one simple changes. Specify the property-names from object and assign its value to global variable on the left hand side. And enclose the entire answer in parenthesis.

  ({count:this.count, moves: this.moves} = getParameters(mode));

So, this.count & this.moves gets the value of count & moves respectively.

If, you have any better solution please post. Also, if you have any suggestion on better ways to construct the object in the getParameters() function and return, then please share.


For your specific use case, you could:

Object.assign(this, getParameters(mode));

Object.assign accepts two or more parameters, identified as (target, ...sources). The pseudo-code for this method looks like:

function assign(target, ...sources) {
    for(const source of sources) {
        Object.entries(source).forEach(value => (target[value[0]] = value[1]));

(Note: the actual code is much more complicated, the above is a simplified illustration of how Object.assign works)

This has the advantage of not having to specify individual parameters if you add more properties to your return value.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .