gpt4 book ai didi

JavaScript 符号类型 : (non-string object keys)

转载 作者:数据小太阳 更新时间:2023-10-29 04:42:38 31 4
gpt4 key购买 nike

什么是“Symbol”javascript 类型 as mentioned in this ECMAScript 6 draft specification

引用规范:

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.

我认为对象键只是字符串,而且我并不孤单。引用this accepted SO answer :

…object keys are always strings…

你能解释一下符号类型是什么,并演示它的用法吗?我正在尝试理解规范。

谢谢!

最佳答案

I thought object keys were strings only

您是对的,但这仅适用于 EcmaScript 5。 ES 6/harmony 是新事物的草稿!

I'm trying to make sense of the spec

这只是草稿,rapidly changing .符号的使用方式以及它们是否或如何由任意脚本创建似乎还没有解决(扫描 versions 以了解更改)。

如果向下滚动到 that document 的末尾(甚至在附件 F 下方),例如,您将看到 第 8.4.4 节:特殊符号对象 已被移出那里。它指出

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

您可以在 section 8.1.7.4 Well-Known Symbols and Intrinsics 看到它们的使用例如。对于 Symbol 构造函数的建议用途(以及仍然存在的问题/ Unresolved 问题),请查看 these strawman页面或 this wiki site .

关于JavaScript 符号类型 : (non-string object keys),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17759780/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com