javascript 符号
by Vali Shah
通过瓦利沙阿
JavaScript符号快速概述 (A quick overview of JavaScript symbols)
符号 (Symbols)
Symbols are new primitive type introduced in ES6. Symbols are completely unique identifiers. Just like their primitive counterparts (Number, String, Boolean), they can be created using the factory function Symbol()
which returns a Symbol.
符号是新的原始符号 ES6中引入的类型。 符号是完全唯一的标识符。 就像它们的原始副本( Number , String , Boolean )一样,可以使用返回符号的工厂函数Symbol()
创建它们。
const symbol = Symbol('description')
const symbol = Symbol('description')
Every time you call the factory function, a new and unique symbol is created. The optional string-valued parameter is a descriptive string that is shown when printing the symbol.
每次调用工厂函数时,都会创建一个新的唯一符号。 可选的字符串值参数是在打印符号时显示的描述性字符串。
> symbol
Symbol(descripti
on)
> symbol
Symbol(descripti
)
Every symbol returned by Symbol()
is unique, so every symbol has its own identity:
Symbol()
返回的每个符号都是唯一的,因此每个符号都有自己的标识:
> Symbol() === Symbol()
fa
lse
> Symbol() === Symbol()
fa
LSE
You can see that symbols are primitive if you apply the typeof
operator to one of them — it will return a new symbol-specific result:
如果将typeof
运算符应用于其中之一,则可以看到符号是原始的,它将返回一个特定于符号的新结果:
>typeof symbol
'symb
ol'
>typeof symbol
'symb
ol'
用例:将符号用作非公共属性的键 (Use Case: Symbols as keys of non-public properties)
Whenever there are inheritance hierarchies in JavaScript, you have two kinds of properties (e.g. created via classes, a purely prototypal approach):
每当JavaScript中有继承层次结构时,您就会拥有两种属性(例如,通过类创建的,纯粹的原型方法):
Public properties are seen by clients of the code
代码的客户可以看到公共属性
Private properties are used internally within the pieces that make up the inheritance hierarchy (e.g. classes, objects).
私有属性在组成继承层次结构的各个部分内部使用(例如,类,对象)。
For usability's sake, public properties usually have string keys. But for private properties with string keys, accidental name clashes can become a problem. Therefore, symbols are a good choice.
出于可用性考虑,公共属性通常具有字符串键。 但是对于具有字符串键的私有属性,意外的名称冲突可能会成为一个问题。 因此,符号是一个不错的选择。
For example, in the following code, symbols are used for private properties _counter
and _action
:
例如,在以下代码中,符号用于私有属性_counter
和_action
:
Note that symbols only protect you from name clashes, not from unauthorized access. You can find out all an object’s property keys — including symbols — via the following:
请注意,符号只能保护您免受名称冲突的侵害,而不能防止未经授权的访问。 您可以通过以下方式找到对象的所有属性键(包括符号):
我们真的需要符号吗? (Do we really need symbols?)
Use symbols when your requirement is one of these:
当您的要求是下列条件之一时,请使用符号:
Enum: To allow you to define constants with semantic names and unique values.
枚举:允许您使用语义名称和唯一值定义常量。
const directions = { UP : Symbol( ‘UP’ ), DOWN : Symbol( ‘DOWN’ ), LEFT : Symbol( ‘LEFT’ ), RIGHT: Symbol( ‘RIGHT’ )};
Name Clashes: when you wanted to prevent collisions with keys in objects
名称冲突:当您想防止与对象中的按键发生冲突时
Privacy: when you don’t want your object properties to be enumerable
隐私:当您不希望对象属性枚举时
Protocols: To define how an object can be iterated.
协议:定义如何迭代对象。
Imagine, for instance, a library like
想象一下,例如一个图书馆
dragula
defining a protocol throughSymbol.for(dragula.moves)
. You can add a method on thatSymbol
to any DOM element. If a DOM element follows the protocol, thendragula
could call theel[Symbol.for('dragula.moves')]()
user-defined method to assert whether the element can be moved.dragula
通过Symbol.for(dragula.moves)
定义协议。 您可以在该Symbol
上添加一个方法到任何DOM元素。 如果DOM元素遵循该协议,则dragula
可以调用el[Symbol.for('dragula.moves')]()
用户定义的方法来断言该元素是否可以移动。Well-known Symbols: In addition to user-defined symbols, JavaScript has some built-in symbols. These represent internal language behaviors which were not exposed to developers in <ES5. More information here.
众所周知的符号:除了用户定义的符号外,JavaScript还有一些内置符号。 这些代表了在<ES5中未暴露给开发人员的内部语言行为。 更多informati 上^ h e重新。
结论 (Conclusion)
Symbols
in Javascript can provide access level uniqueness to objects. It's worthwhile for all developer to have a basic understanding of them and their various use-cases.
Javascript中的Symbols
可以为对象提供访问级别的唯一性。 对于所有开发人员来说,对它们及其各种用例有一个基本的了解是值得的。
code = coffee + developer
code = co ffee + de veloper
翻译自: https://www.freecodecamp.org/news/how-did-i-miss-javascript-symbols-c1f1c0e1874a/
javascript 符号