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

Detailed explanation of how to view the number of MySQL server threads

This article uses an example to describe how to v...

Methods for defragmenting and reclaiming space in MySQL tables

Table of contents Causes of MySQL Table Fragmenta...

Basic usage of wget command under Linux

Table of contents Preface 1. Download a single fi...

How to maintain MySQL indexes and data tables

Table of contents Find and fix table conflicts Up...

Docker cleaning killer/Docker overlay file takes up too much disk space

[Looking at all the migration files on the Intern...

About VUE's compilation scope and slot scope slot issues

What are slots? The slot directive is v-slot, whi...

MySQL subqueries and grouped queries

Table of contents Overview Subqueries Subquery Cl...

Analysis of MySQL's method of exporting to Excel

This article describes how to use MySQL to export...

Summary of basic SQL statements in MySQL database

This article uses examples to describe the basic ...

What is web design

<br />Original article: http://www.alistapar...

Detailed explanation of built-in methods of javascript array

Table of contents 1. Array.at() 2. Array.copyWith...

How to use module fs file system in Nodejs

Table of contents Overview File Descriptors Synch...

An example of using Lvs+Nginx cluster to build a high-concurrency architecture

Table of contents 1. Lvs Introduction 2. Lvs load...

Using Docker to create static website applications (multiple ways)

There are many servers that can host static websi...

Zabbix configuration DingTalk alarm function implementation code

need Configuring DingTalk alarms in Zabbix is ​​s...