ES6:使用符号作为键破坏对象 [英] ES6: destructuring an object with symbols as keys

查看:80
本文介绍了ES6:使用符号作为键破坏对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含符号作为键的对象.在这种情况下,我该如何破坏分配?

I have an object that contains symbols as keys. How do I do destructuring assignment in this case?

let symbol = Symbol()
let obj = {[symbol]: ''}
let { /* how do I create a variable here, that holds the value of [symbol] property? */ } = obj

我需要知道是否可行,我确实知道明显而简单的解决方法,但这不是我要的.

I need to know if this possible, I do know the obvious and simple workarounds, but that's not what I am asking.

UPD.有趣的是,我知道该怎么做,但是打字稿产生了错误,我以为我在JS中做错了什么. 这是针对打字稿用户的一种解决方法.

UPD. Funny enough I knew how to do that but typescript produced errors, and I thought I did something wrong in JS. Here's a fix for typescript users.

推荐答案

使用别名(请参见

let symbol = Symbol()
let obj = { [symbol] : 'value'}
let { [symbol]: alias } = obj

console.log(alias)

这篇关于ES6:使用符号作为键破坏对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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