JavaScript符号类型:(非字符串对象键) [英] JavaScript Symbol type: (non-string object keys)

查看:179
本文介绍了JavaScript符号类型:(非字符串对象键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是SymbolJavaScript类型如上所述在这个ECMAScript 6草案规范中



引用规范:


符号类型是可以用作Object属性键的所有非String值的集合。



每个可能的符号值都是唯一的和不可变的



符号值具有一个名为[[Private]]的可观察属性,其不可变值为true或false。私人符号是一个符号值,其[[Private]]属性的值为true。


我以为对象键只是字符串,我并不孤单要引用此接受的答案


...对象键始终是字符串...


可以解释符号类型是什么,并展示其用法。我试图理解这个规范。



谢谢!

解决方案

我认为对象键只是字符串


你是对的,但是对于只有EcmaScript 5。 ES 6 /和谐是一个新的东西的草稿!


我试图理解规范


这只是草稿,快速变化。如何使用符号以及是否或如何通过任意脚本创建它们似乎还没有解决(请浏览 version 进行更改。



如果向下滚动到这个文件(甚至在附件F以下),例如,您将看到一个第8.4.4节:符号外来物体那已经搬到那里了它表示


异乎寻常的符号对象为所有必要的内部方法提供了替代定义。


您可以在第8.1节例如.7.4 已知符号和Intrinsics 。对于 Symbol 构造函数的建议使用(和仍然存在的问题/公开问题),请查看这些 strawman 页面或此维基站点


What is the "Symbol" javascript type as mentioned in this ECMAScript 6 draft specification?

To quote the spec:

The Symbol type is the set of all non-String values that may be used as the key of an Object property.

Each possible Symbol values is unique and immutable.

Symbol values have a single observable attribute called [[Private]] whose immutable value is either true or false. A private symbol is a Symbol value whose [[Private]] attribute has the value true.

I thought object keys were strings only, and I'm not alone. To quote this accepted SO answer:

…object keys are always strings…

Could you explain what the symbol type is, and demonstrate its usage. I'm trying to make sense of the spec.

Thanks!

解决方案

I thought object keys were strings only

You're right, but that was true for EcmaScript 5 only. ES 6 / harmony is a draft for something new!

I'm trying to make sense of the spec

It's a draft only, rapidly changing. How symbols are used and whether or how they can be created by arbitrary scripts does not seem to have settled yet (scan through the versions for changes).

If you scroll down to the very end of that document (even below Annex F), you for example will see a Section 8.4.4: Symbol Exotic Objects that has been moved out there. It states

Exotic Symbol objects provide alternative definitions for all of the essential internal methods.

You can see them used at section 8.1.7.4 Well-Known Symbols and Intrinsics for example. For proposed uses (and still existing problems / open questions) of Symbol constructors have a look at these strawman pages or this wiki site.

这篇关于JavaScript符号类型:(非字符串对象键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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