简体   繁体   English

带点符号的ES6符号

[英]ES6 Symbols with Dot Notation

In the examples I've seen so far using Symbols in ES6, you have to access symbol properties in Object literals using bracket notation: 在到目前为止我在ES6中使用Symbols看到的示例中,您必须使用方括号表示法访问Object常量中的symbol属性:

let mySymbol = Symbol("mySymbol");

let someObject = {
  [mySymbol]: "someValue"
};

console.log(someObject[mySymbol]); // "someValue"

Is there a way to define and access symbol properties using dot notation? 有没有一种方法可以使用点表示法定义和访问符号属性?

No. Symbols must be accessed using bracket notation. 否。必须使用方括号符号访问符号。

Dot notation is only used for string keys that follow certain rule patterns, mostly about being a valid identifier. 点表示法仅用于遵循某些规则模式的string键,大多数情况下是关于有效标识符。

Symbols are not strings, they are a whole something else entirely. 符号不是字符串,它们是一个整体。

Short rational: One of the design goals of symbols is that they can not clash with property names, that makes them safe to use. 简短的合理性:符号的设计目标之一是它们不能与属性名称冲突,这使得它们可以安全使用。

So, if you had an object like this 因此,如果您有这样的对象

var = {
  prop1: "Value"
};

And you created a symbol called prop1 , how could you tell the two apart, and access them differently, using just object notation? 然后,您创建了一个名为prop1的符号,如何仅使用对象符号将两者分开,并以不同的方式访问它们?

No. Dot notation is reserved for identifiers which resolve to property names (strings). 否。点符号保留用于解析为属性名称 (字符串)的标识符。 And this won't change for backward compatibility reasons. 而且由于向后兼容的原因,这不会改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM