what is the difference between test1 and test2?
An assignment expression always returns the right hand side value. return (… = obj2)
will always return the obj2
value no matter what …
is.
how to return a destructured object without declaring them as variables?
You seem to be confused about what destructuring does. There is no such thing as a "destructured object", when you de-structure the object it's not an object any longer. Its invidivual parts are getting assigned to plain variables.
In your test2
function, you then create a new object with the values from these variables, using an object literal with shorthand syntax.
There is no way to combine a destructuring target and an object literal into one expression.
If you don't want to introduce individual variables with destructuring (either in the parameter declaration or a separate variable declaration), just access the argument object properties directly as usual:
function test3(obj) {
return { amount: obj[0], balance: obj[1], products: obj[2] };
}
(Taken from @user3840170's comment)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…