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
This article uses examples to illustrate the usag...
There is a difference between src and href, and t...
Event Description onactivate: Fired when the objec...
All blogs listed below are original and uniquely ...
Table of contents 1. Closure 2. Closure usage sce...
System version [root@ ~]# cat /etc/redhat-release...
Add in the <Head> tag <meta http-equiv=&q...
Two small problems, but they bothered me for a lon...
1. TCP Wrappers Overview TCP Wrappers "wraps...
This article shares the specific code for using j...
background When developing a feature similar to c...
Table of contents Drop-down multiple-select box U...
01. Command Overview md5sum - Calculate and verif...
This article introduces the flex layout to achiev...
Nginx first decides which server{} block in the c...