toString()
method returns a string representing the object.
obj.toString()
A string representing the object.
Every object has a
toString()
method that is automatically called when the object is to be represented as a text value or when an object is referred to in a manner in which a string is expected. By default, the
toString()
method is inherited by every object descended from
Object
. If this method is not overridden in a custom object,
toString()
returns "
[object
type
]
", where
type
is the object type. The following code illustrates this:
const o = new Object(); o.toString(); // returns [object Object]
注意:
Starting in JavaScript 1.8.5,
toString()
called on
null
返回
[object
Null
]
,和
undefined
返回
[object
Undefined
]
, as defined in the 5
th
Edition of ECMAScript and subsequent Errata.
见
使用
toString()
to detect object class
.
For Numbers and BigInts
toString()
takes an optional parameter
radix
the value of radix must be minimum 2 and maximum 36.
By using
radix
you can also convert base 10 numbers (like 1,2,3,4,5,.........) to another base numbers, in example below we are converting base 10 number to a base 2 (binary) number
let baseTenInt = 10; console.log(baseTenInt.toString(2)); // Expected output is "1010"
and same for big integers
let bigNum = BigInt(20); console.log(bigNum.toString(2)); // Expected output is "10100"
Some common radix are
toString
方法
You can create a function to be called in place of the default
toString()
method. The
toString()
method takes no arguments and should return a string. The
toString()
method you create can be any value you want, but it will be most useful if it carries information about the object.
The following code defines the
Dog
object type and creates
theDog
, an object of type
Dog
:
function Dog(name, breed, color, sex) {
this.name = name;
this.breed = breed;
this.color = color;
this.sex = sex;
}
theDog = new Dog('Gabby', 'Lab', 'chocolate', 'female');
If you call the
toString()
method on this custom object, it returns the default value inherited from
Object
:
theDog.toString(); // returns [object Object]
The following code creates and assigns
dogToString()
to override the default
toString()
method. This function generates a string containing the
name
,
breed
,
color
,和
sex
of the object, in the form "
property = value;
".
Dog.prototype.toString = function dogToString() {
const ret = 'Dog ' + this.name + ' is a ' + this.sex + ' ' + this.color + ' ' + this.breed;
return ret;
}
Or, using ES6
template strings
:
Dog.prototype.toString = function dogToString() {
return `Dog ${this.name} is a ${this.sex} ${this.color} ${this.breed}`;
}
With the preceding code in place, any time
theDog
is used in a string context, JavaScript automatically calls the
dogToString()
function, which returns the following string:
"Dog Gabby is a female chocolate Lab"
toString()
to detect object class
toString()
can be used with every object and (by default) allows you to get its class.
To use the
Object.prototype.toString()
with every object, you need to call
Function.prototype.call()
or
Function.prototype.apply()
on it, passing the object you want to inspect as the first parameter (called
thisArg
).
const toString = Object.prototype.toString; toString.call(new Date); // [object Date] toString.call(new String); // [object String] toString.call(Math); // [object Math] // Since JavaScript 1.8.5 toString.call(undefined); // [object Undefined] toString.call(null); // [object Null]
使用
toString()
in this way is unreliable; objects can change the behavior of
Object.prototype.toString()
by defining a
Symbol.toStringTag
property, leading to unexpected results. For example:
const myDate = new Date(); Object.prototype.toString.call(myDate); // [object Date] myDate[Symbol.toStringTag] = 'myDate'; Object.prototype.toString.call(myDate); // [object myDate] Date.prototype[Symbol.toStringTag] = 'prototype polluted'; Object.prototype.toString.call(new Date()); // [object prototype polluted]
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Object.prototype.toString' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
toString()
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
完整支持
Object.prototype.toSource()
Object.prototype.valueOf()
Number.prototype.toString()
Symbol.toPrimitive
Object
Object.assign()
Object.create()
Object.defineProperties()
Object.defineProperty()
Object.entries()
Object.freeze()
Object.fromEntries()
Object.getOwnPropertyDescriptor()
Object.getOwnPropertyDescriptors()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Object.getPrototypeOf()
Object.is()
Object.isExtensible()
Object.isFrozen()
Object.isSealed()
Object.keys()
Object.preventExtensions()
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.seal()
Object.setPrototypeOf()
Object.values()
Function