For example if I have two objects:
var foo = {
x: "bar",
y: "baz"
}
and
var oof = {}
and I wanted to transfer the x and y values from foo to oof. Is there a way to do that using the es6 destructuring syntax?
perhaps something like:
oof{x,y} = foo
This works in chrome 53.0.2785.89
This is kind of cheating, but you can do something like this...
In practice, I think you'd rarely want to use that though. The following is MUCH more clear... but not nearly as fun.
Another completely different route, which includes mucking with the prototype (careful now...):
It's totally possible. Just not in one statement.
(Do note the parenthesis around the statement.) But keep in mind legibility is more important than code-golfing :).
Source: http://exploringjs.com/es6/ch_destructuring.html#sec_assignment-targets
This is the most readable and shortest solution I could come up with:
It will output
{ isValidDate: 'yes' }
It would be nice to some day be able to say something like
let newProps = ({ isValidDate } = props)
but unfortunately it is not something ES6 supports.It's not a beautiful way, nor I recommend it, but it's possible this way, just for knowledge.
Other than
Object.assign
there is the object spread syntax which is a Stage 2 proposal for ECMAScript.But to use this feature you need to use
stage-2
ortransform-object-rest-spread
plugin for babel. Here is a demo on babel withstage-2