Symbol.prototype[@@toPrimitive]()

Symbol 值的 [@@toPrimitive]() 方法返回该符号值。

¥The [@@toPrimitive]() method of Symbol values returns this symbol value.

语法

¥Syntax

js
symbolValue[Symbol.toPrimitive](hint)

参数

¥Parameters

hint

一个字符串值,指示要返回的原始值。该值被忽略。

返回值

¥Return value

指定 Symbol 对象的原始值。

¥The primitive value of the specified Symbol object.

描述

¥Description

Symbol[@@toPrimitive]() 方法将 Symbol 对象的原始值作为 Symbol 数据类型返回。未使用 hint 参数。

¥The [@@toPrimitive]() method of Symbol returns the primitive value of a Symbol object as a Symbol data type. The hint argument is not used.

JavaScript 调用 [@@toPrimitive]() 方法将对象转换为原始值。你很少需要自己调用 [@@toPrimitive]() 方法;当遇到需要原始值的对象时,JavaScript 会自动调用它。

¥JavaScript calls the [@@toPrimitive]() method to convert an object to a primitive value. You rarely need to invoke the [@@toPrimitive]() method yourself; JavaScript automatically invokes it when encountering an object where a primitive value is expected.

示例

¥Examples

使用@@toPrimitive

¥Using @@toPrimitive

js
const sym = Symbol("example");
sym === sym[Symbol.toPrimitive](); // true

规范

Specification
ECMAScript Language Specification
# sec-symbol.prototype-@@toprimitive

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also