Symbol.toPrimitive 是一个内置的 Symbol 值,它是作为对象的函数值属性存在的,当一个对象转换为对应的原始值时,会调用此函数。
描述
在 Symbol.toPrimitive 属性(用作函数值)的帮助下,一个对象可被转换为原始值。该函数被调用时,会被传递一个字符串参数 hint ,表示要转换到的原始值的预期类型。 hint 参数的取值是 "number"、"string" 和 "default" 中的任意一个。
Symbol.toPrimitive 属性的属性特性: |
|
|---|---|
| writable | false |
| enumerable | false |
| configurable | false |
示例
下面的例子展示了, Symbol.toPrimitive 属性是如何干扰一个对象转换为原始值时输出的结果的。
// 一个没有提供 Symbol.toPrimitive 属性的对象,参与运算时的输出结果
var obj1 = {};
console.log(+obj1); // NaN
console.log(`${obj1}`); // "[object Object]"
console.log(obj1 + ""); // "[object Object]"
// 接下面声明一个对象,手动赋予了 Symbol.toPrimitive 属性,再来查看输出结果
var obj2 = {
[Symbol.toPrimitive](hint) {
if (hint == "number") {
return 10;
}
if (hint == "string") {
return "hello";
}
return true;
}
};
console.log(+obj2); // 10 -- hint 参数值是 "number"
console.log(`${obj2}`); // "hello" -- hint 参数值是 "string"
console.log(obj2 + ""); // "true" -- hint 参数值是 "default"
规范
| 规范 | 状态 | 注释 |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) Symbol.toPrimitive |
Standard | 原始定义 |
| ECMAScript Latest Draft (ECMA-262) Symbol.toPrimitive |
Draft |
浏览器兼容性
Update compatibility data on GitHub
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
toPrimitive |
Chrome Full support 47 | Edge Full support 15 | Firefox Full support 44 | IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 47 | Chrome Android Full support 47 | Firefox Android Full support 44 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 5.0 | nodejs Full support 6.0.0 |
Legend
- Full support
- Full support
- No support
- No support