In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is the role of the toString method in js, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
First, the three functions of toString method
1. Returns a [string] that [represents an object]
two。 Type of detection object
Object.prototype.toString.call (arr) = = "[object Array]"
3. Returns the string of the number corresponding to the base.
Console.log (10.toString (2)) / / 10 is designed for binary '1010'
In js, it contains binary, octal, decimal, and 18.
2. Return a [string] that represents the object.
Object.prototype.toString ()
1.toString is a method that belongs to the Object prototype.
Each object has a toString () method. By default, the toString () method is inherited by each object. If the toString is not overridden by the defined object. ToString returns'[object type] 'where type is the type of object and the value of type can be Object
Code:
Class Person {constructor (name,age) {this.name=name this.age=age}} let zs=new Person ('Zhang San', 18) console.log (zs.toString ()) / / [object Object]
From the output statement above, we can determine.
What is returned is indeed a [string] that [represents the object].
3. Custom toString ()
We can also define a method to override the default toString method.
The custom toString () method cannot pass in parameters and must return a string. The defined toString can return any value we need, and if it can attach any information about the object, it will become very useful.
The code is as follows:
Class Person {constructor (name,age) {this.name=name this.age=age} / / rewrite Object.prototype.toString () toString () {return `Person {name=$ {this.name}, age=$ {this.age}} `} let zs=new Person ('Zhang San', 18) console.log (zs.toString ()) / / Person {name= Zhang San, age=18}
Many of JavaScript's built-in objects rewrite this function to better suit its own functional needs.
1. Each element of Array is converted into a string and concatenated in turn, with an English comma between the two elements
2.Boolean returns "true" if the Boolean value is true. Otherwise, "false" is returned.
3.Date returns a text representation of the date.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.