Object.prototype.valueOf()The valueOf of an object is designed to return the primitive value of the object, and will automatically perform conversion of the object into its primitive value wherever it is needed. Click here for details. Object.prototype.toString()The toString() method returns a string representation of the object and is automatically performed where an object is expected to be converted to a string. The default toString() method of an object returns [object type], where type is the name of the object's constructor. Click here for details. Symbol.toPrimitive
let obj = { [Symbol.toPrimitive](hint) { switch (hint) { case 'number': return 123; case 'string': return 'str'; case 'default': return 'default'; default: throw new Error(); } } }; 2 * obj // 246 3 + obj // '3default' obj == 'default' // true String(obj) // 'str' Object conversion primitive valueThe above three methods are triggered when the object is expected to be converted into some primitive value. 1. Expected to be converted to string type
Where output is performed, such as alert() String(obj) let a = { toString () { return '2' } } console.log(String(a)) // 2 String concatenation (+) operation let a = { toString () { return '2' } } console.log(a + 'vv') Template Strings let a = { [Symbol.toPrimitive] (hint) { console.log(hint) // string return 2 } } console.log(`Are you old ${a}?`) // Are you old 2? 2. Expected to be converted to a numeric type
division: let a = { valueOf () { return 2 } } console.log(2 / a, a / 2) // 1 1 Number(obj): let a = { [Symbol.toPrimitive] (hint) { console.log(hint) // number return 2 } } console.log(Number(a)) // 2 Positive and negative signs (note that it is not an addition or subtraction operation): let a = { [Symbol.toPrimitive] (hint) { console.log(hint) // number return 2 } } console.log(+a) // 2 console.log(-a) // -2 3. Expected to be converted to the default type (other)
Numeric addition (i.e. the object being added is a numeric type): let a = { [Symbol.toPrimitive] (hint) { console.log(hint) // default return 2 } } console.log(1 + a) // 3
Boolean operations : all objects are converted to true; let a = { [Symbol.toPrimitive] (hint) { console.log(hint) // No trigger return false } } console.log(Boolean(a), a && 123) // true 123
The order in which the three methods are triggeredFirst, determine whether the object has the Symbol.toPrimitive(hint) method. If so, execute the method. If not, execute the following steps. If it is expected to be converted into a string type, the toString() method is executed first; If it is expected to be converted to the default type or numeric type, the valueOf() method is executed first: Note : If there is no valueOf() method, but a toString() method is defined, the toString() method will be executed; SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Css3 realizes seamless scrolling and anti-shake
>>: Solution to Mysql binlog log file being too large
When using element-ui, there is a commonly used c...
This article shares with you the MySQL 8.0.17 ins...
Programs in Docker containers often need to acces...
1. Docker startup problem: Problem Solved: You ne...
Today I used a virtual machine to do an experimen...
Table of contents Multi-table join query Inner Jo...
Refer to the tutorial on setting up FTP server in...
The happiest thing that happens in a production e...
Port mapping is not the only way to connect Docke...
We don't need to elaborate too much on the ad...
Table of contents Linux environment variables and...
This is an important (and wonderful) topic for Li...
This article shares the specific code of Vue to s...
1. flex-grow, flex-shrink, flex-basis properties ...
Use native js to implement a simple calculator (w...