Symbols are private keys that replace magic names. They prevent using a simple string to reference the field, so only consumers with the symbol can gain access.
Some symbols are used to indicate particular behaviors to the runtime (like Symbol.iterator
, which acts much like a pre-shared secret), while others can be allocated by the library and used to effectively hide fields.
In general, symbols are intended as a replacement for magical names. Rather than having a properties simply called 'foo', you can allocate a symbol const foo = Symbol()
and pass that selectively. This allows the runtime to allocate Symbol.iterator
when it starts up and guarantees that anyone trying to implement an iterable does so in a consistent fashion.
The runtime could use symbols to optimize access to certain fields, if it felt the need to, but doesn't have to.
You can use symbols to direct consumers to a particular method, depending on their usage. For example, if you had a library that could return a synchronous iterable or a generator, depending on the client's async support, you could:
const syncIterable = Symbol();
const asyncIterable = Symbol();
class Foo {
static getIterable(async = false) {
return async ? asyncIterable : syncIterable;
}
[syncIterable]() {
return new SyncFoo();
}
[asyncIterable]() {
return new AsyncFoo();
}
}
let foo = new Foo();
for (let x of foo[Foo.getIterable(true)]()) {
// could be a iterator, could be a generator
}
That's a rather contrived example, but shows how a library can use symbols to selectively provide access to users.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…