In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the original value of JavaScript object conversion". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what the original value of JavaScript object conversion is.
Object.prototype.valueOf ()
The valueOf of an object is designed to return the original value of the object and is automatically executed where the object needs to be converted to the original value. More details here.
Object.prototype.toString ()
The toString () method returns a string representing the object, which is automatically executed where the object is expected to be converted to a string. The object's default toString () method returns [object type], and this type is the name of the object constructor. More details here.
Symbol.toPrimitive
The Symbol.toPrimitive (hint) method works the same as valueOf (), but takes precedence over valueOf (), and it also accepts a parameter, hint, that represents the specific type of original value you want to convert, in the following ways:
Number: numeric type
String: string type
Default: default
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 / / 2463 + obj / / '3default'obj = =' default' / / trueString (obj) / / 'str' object conversion original value
All three methods are triggered when the object is expected to be converted to some original value.
1. Expected to be converted to a string type
The corresponding hint type is string
Where the output is made, 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 string
Let a = {[Symbol.toPrimitive] (hint) {console.log (hint) / / string return 2}} console.log (`are you the old ${a}? `) / / are you the boss? two。 Expected to be converted to a numeric type
The corresponding hint type is numbe
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
Plus or minus sign (note that it is not an addition or subtraction operation):
Let a = {[Symbol.toPrimitive] (hint) {console.log (hint) / / number return 2}} console.log (+ a) / / 2console.log (- a) / /-23. Expected to be converted to the default type (other)
The corresponding hint type is default
Numeric addition (that is, the party added to the object is the numeric type):
Let a = {[Symbol.toPrimitive] (hint) {console.log (hint) / / default return 2}} console.log (1 + a) / / 3
This is a bit of a surprise. I thought that the type expected to be converted in this case should be a numeric type, but it is actually default.
Boolean operations: all objects are converted to true
Let a = {[Symbol.toPrimitive] (hint) {console.log (hint) / / No return false}} console.log (Boolean (a), a & & 123) / / true 123
Boolean operations include =
The trigger sequence of the three methods
First determine whether the object has a Symbol.toPrimitive (hint) method, and if so, execute the method, and if not, perform the following steps
If it is expected to be converted to a string type, the toString () method takes precedence
If you expect to be converted to the default type or numeric type, the valueOf () method takes precedence:
Note: if there is no valueOf () method, but the toString () method is defined, the toString () method will be executed
Thank you for your reading, these are the contents of "what is the original value of JavaScript object conversion?" after the study of this article, I believe you have a deeper understanding of what the original value of JavaScript object conversion is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.