toString()
method returns a string representing the specified
Error
对象。
e.toString()
A string representing the specified
Error
对象。
Error
object overrides the
Object.prototype.toString()
method inherited by all objects. Its semantics are as follows (assuming
Object
and
String
have their original values):
Error.prototype.toString = function() {
'use strict';
var obj = Object(this);
if (obj !== this) {
throw new TypeError();
}
var name = this.name;
name = (name === undefined) ? 'Error' : String(name);
var msg = this.message;
msg = (msg === undefined) ? '' : String(msg);
if (name === '') {
return msg;
}
if (msg === '') {
return name;
}
return name + ': ' + msg;
};
var e = new Error('fatal error');
console.log(e.toString()); // 'Error: fatal error'
e.name = undefined;
console.log(e.toString()); // 'Error: fatal error'
e.name = '';
console.log(e.toString()); // 'fatal error'
e.message = undefined;
console.log(e.toString()); // ''
e.name = 'hello';
console.log(e.toString()); // 'hello'
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Error.prototype.toString' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
toString
|
Chrome 1 | Edge 12 | Firefox 1 | IE 6 | Opera 4 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持
Error
Error.prototype.toSource()
Error.prototype.toString()
EvalError
InternalError
RangeError
ReferenceError
SyntaxError
TypeError
URIError
Function
Object
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.setPrototypeOf()