3 functions of toString method in js

3 functions of toString method in js

1. Three functions of toString method

1. Return a string representing an object

2. Type of detection object

Object.prototype.toString.call(arr) == = "[object Array]"

3. Return the string corresponding to the number.

console.log(10.toString(2)) //10 is specifically for binary '1010'

js contains binary, octal, decimal and octal bases.

2. Return a string representing an object

Object.prototype.toString()

1. toString is a method on the Object prototype.

Every object has a toString() method. By default, toString()方is inherited by every object. If toString is not overridden by the object defined. toString returns '[object type]' where type is the type of the object. The value of type can be Object.

Code:

class Person{
  constructor(name,age){
    this.name=name
    this.age=age
  }
}
let zs=new Person('张三',18)
console.log( zs.toString() ) // [object Object]


  • Through the above output statement, we can be sure.
  • What is returned is indeed a string representing an object.

3. Custom toString()

We can also define a method to override the default toString method.

A custom toString() method cannot take any arguments and must return a string. The defined toString can return any value we need. If it can carry 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
  }
  // Override Object.prototype.toString()
  toString(){
    return `Person{name=${this.name},age=${this.age}}`
  }
}
let zs=new Person('张三',18)
console.log( zs.toString() ) //Person{name=张三,age=18}

Many built-in objects in JavaScript have rewritten this function to implement functions that are more suitable for their own needs.

  • 1. Convert each element of Array to a string and concatenate them one by one, using commas as separators between two elements.
  • 2. Boolean If the Boolean value is true, it returns "true". Otherwise returns "false"".
  • 3. Date returns the text representation of a date.

This is the end of this article about the three functions of the toString method in js. For more relevant content about the toString method in js, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JavaScript Number object toString() method
  • Use toString() method in JavaScript to return time as string
  • Detailed explanation of the use of toString() method in JavaScript
  • JavaScript defines objects through function and adds toString() method to objects

<<:  Detailed explanation of the usage of the rare tags fieldset and legend

>>:  Introduction to installing and configuring JDK under CentOS system

Recommend

Comprehensive understanding of line-height and vertical-align

Previous words Line-height, font-size, and vertica...

HTML basic summary recommendation (text format)

HTML text formatting tags 標簽 描述 <b> 定義粗體文本 ...

JavaScript implements draggable progress bar

This article shares the specific code of JavaScri...

Example sharing of anchor tag usage in HTML

Anchor tag usage: Linking to a specific location i...

Detailed explanation of global parameter persistence in MySQL 8 new features

Table of contents Preface Global parameter persis...

Tools to convert static websites into RSS

<br /> This article is translated from allwe...

Quickly install MySQL5.7 compressed package on Windows

This article shares with you how to install the M...

MySQL learning database search statement DQL Xiaobai chapter

Table of contents 1. Simple retrieval of data 2. ...

Analysis of parameter transfer process of driver module in Linux

Declare the parameter name, type and permission y...

How to use react-color to implement the front-end color picker

background We can use react-color to implement th...

Install docker offline by downloading rpm and related dependencies using yum

You can use yum to install all dependencies toget...

What are the advantages of MySQL MGR?

MGR (MySQL Group Replication) is a new feature ad...

Solve the matching problem in CSS

Problem Description As we all know, when writing ...

Introduction to network drivers for Linux devices

Wired network: Ethernet Wireless network: 4G, wif...