Symbol.keyFor()

Symbol.keyFor() 静态方法从全局符号注册表中检索给定符号的共享符号键。

¥The Symbol.keyFor() static method retrieves a shared symbol key from the global symbol registry for the given symbol.

Try it

语法

¥Syntax

js
Symbol.keyFor(sym)

参数

¥Parameters

sym

符号,必填。用于查找密钥的符号。

返回值

¥Return value

一个字符串,表示给定符号的密钥(如果在 全球登记处 上找到);否则,undefined

¥A string representing the key for the given symbol if one is found on the global registry; otherwise, undefined.

示例

¥Examples

使用 keyFor()

¥Using keyFor()

js
const globalSym = Symbol.for("foo"); // create a new global symbol
Symbol.keyFor(globalSym); // "foo"

const localSym = Symbol();
Symbol.keyFor(localSym); // undefined

// well-known symbols are not symbols registered
// in the global symbol registry
Symbol.keyFor(Symbol.iterator); // undefined

规范

Specification
ECMAScript Language Specification
# sec-symbol.keyfor

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also