类型错误:WeakSet 键/WeakMap 值 'x' 必须是对象或未注册的符号
当无效类型的值用作 WeakSet
中的键或 WeakMap
中的值时,会发生 JavaScript 异常 "WeakSet 键(或 WeakMap 值)'x' 必须是对象或未注册的符号"。
¥The JavaScript exception "WeakSet key (or WeakMap value) 'x' must be an object or an unregistered symbol" occurs when an value of invalid type is used as a key in a WeakSet
or as a value in a WeakMap
.
信息
¥Message
TypeError: Invalid value used as weak map key (V8-based) TypeError: WeakMap key 1 must be an object or an unregistered symbol (Firefox) TypeError: WeakMap keys must be objects or non-registered symbols (Safari) TypeError: Invalid value used in weak set (V8-based) TypeError: WeakSet value 1 must be an object or an unregistered symbol (Firefox) TypeError: WeakSet values must be objects or non-registered symbols (Safari)
错误类型
什么地方出了错?
¥What went wrong?
WeakSet
和 WeakMap
要求键是可垃圾回收的。只有对象和非注册符号(即 Symbol.for()
未返回的 symbols)才有效。欲了解更多信息,请参阅 内存管理。如果你想添加字符串、数字或其他原始值的键,则应将它们存储在常规 Set
或 Map
中。
¥WeakSet
and WeakMap
require the keys to be garbage collectable. Only objects and non-registered symbols (that is, symbols not returned by Symbol.for()
) are valid. For more information, see Memory management. If you want to add keys that are strings, numbers, or other primitive values, you should store them in a regular Set
or Map
instead.