是否有可能重建到现有的对象上? (Javascript ES6) [英] Is it possible to destructure onto an existing object? (Javascript ES6)

查看:134
本文介绍了是否有可能重建到现有的对象上? (Javascript ES6)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我有两个对象:

  var foo = {
x:bar,
y:baz
}

  var oof = {} 

我想将x和y值从foo转移到oof。有没有办法使用es6解构语法?



也许就像:

  oof {x,y} = foo 


解决方案

否,解构不支持成员表达式,但不支持当前的简单属性名称。 已经在esdiscuss上进行了这样的谈话,但是没有任何建议将会进入您可能可以使用 Object.assign - 如果您不需要所有属性,您仍然可以执行

  var foo = ...,
oof = {};
{
let {x,y} = foo;
Object.assign(oof,{x,y})
}


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

解决方案

No, destructuring does not support member expressions but only plain propertynames at the current time. There have been talks about such on esdiscuss, but no proposals will make it into ES6.

You might be able to use Object.assign however - if you don't need all own properties, you still can do

var foo = …,
    oof = {};
{
    let {x, y} = foo;
    Object.assign(oof, {x, y})
}

这篇关于是否有可能重建到现有的对象上? (Javascript ES6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆