Javascript 为什么错误对象可以打印纯字符串消息

Javascript 为什么错误对象可以打印纯字符串消息,javascript,prototype,Javascript,Prototype,我不想从错误继承或扩展,因此如果您发布的解决方案是从错误继承或扩展的,请不要将我的问题标记为重复 下面是我在Chrome上运行的示例代码。下面的注释console.log是从Chrome开发者工具的控制台选项卡输出的 我想知道的是为什么Error对象可以输出原始字符串消息,而我的CustomError将输出一个对象 是否可以实现像Error这样的对象来输出原始字符串消息 <!DOCTYPE html> <html lang="en"> <he

我不想从
错误继承或扩展,因此如果您发布的解决方案是从
错误继承或扩展的,请不要将我的问题标记为重复

下面是我在Chrome上运行的示例代码。下面的注释
console.log
是从Chrome开发者工具的控制台选项卡输出的

我想知道的是为什么
Error
对象可以输出原始字符串消息,而我的
CustomError
将输出一个对象

是否可以实现像
Error
这样的对象来输出原始字符串消息

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    function CustomError() {
      this.message = "MyErrorMessage";
    }

    var nativeError = new Error();
    var customError = new CustomError();

    console.log(nativeError);
    // Error

    console.log(customError);
    // CustomError {message: "MyErrorMessage"}

    Error.prototype.message = "Modify native error prototype message";
    var nativeErrorAfterModifyPrototypeMessage = new Error();
    console.log(nativeErrorAfterModifyPrototypeMessage);
    // Error: Modify native error prototype message

    CustomError.prototype.message = "Modify custom error prototype message";
    customErrorAfterModifyPrototypeMessage;
    var customErrorAfterModifyPrototypeMessage = new CustomError();
    console.log(customErrorAfterModifyPrototypeMessage);
    // CustomError {message: "MyErrorMessage"}

    customErrorAfterModifyPrototypeMessage.__proto__.message =
      "Modify custom error prototype message";

    console.log(customErrorAfterModifyPrototypeMessage);
    //index.html:37 CustomError {message: "MyErrorMessage"}
  </script>
</html>

文件
函数CustomError(){
this.message=“MyErrorMessage”;
}
var nativeError=新错误();
var customError=new customError();
控制台日志(nativeError);
//错误
console.log(customError);
//自定义错误{消息:“MyErrorMessage”}
Error.prototype.message=“修改本机错误原型消息”;
var nativeErrorRofterModifyPrototypeMessage=新错误();
日志(NativeErrorRafterModifyPrototypeMessage);
//错误:修改本机错误原型消息
CustomError.prototype.message=“修改自定义错误原型消息”;
customErrorAfterModifyPrototypeMessage;
var customErrorAfterModifyPrototypeMessage=new CustomError();
console.log(customErrorAfterModifyPrototypeMessage);
//自定义错误{消息:“MyErrorMessage”}
customErrorAfterModifyPrototypeMessage.\uuuu proto\uuuuu.message=
“修改自定义错误原型消息”;
console.log(customErrorAfterModifyPrototypeMessage);
//html:37自定义错误{消息:“MyErrorMessage”}

原因很简单,因为控制台专门处理错误实例,使用
.toString()
进行格式化。如果您想要这种行为,您必须从错误中继承

开发工具的源代码


来自@Berg评论的答案

您可以随时覆盖
console.log
,并对
CustomError
进行特殊处理

函数CustomError(message=”“){
this.message=消息;
//重写toString以仅返回消息(或任何您希望输出的内容)
this.toString=()=>this.message;
}
const ce=新的自定义错误(“某些消息”);
//这显示了console.log的内置行为
控制台日志(ce);
//保留原来的console.log函数,稍后我们将需要它
console.originallo=console.log;
//使用新函数覆盖console.log
console.log=函数(){
//检查是否有任何参数是CustomError
//并将CustomErrors替换为其字符串表示形式
for(设i=0;i控制台日志(ce)
“我想知道为什么Error对象可以输出原始字符串消息”-这仅仅是因为控制台使用
.toString()
进行格式化,专门处理
Error
的实例。如果你想要这种行为,你必须从
错误继承
。你的意思是如果我不想从
错误继承
,那么
不可能做我想做的事吗?因为这是由Chrome控制的,我能在Chrome源代码中找到这部分源代码吗?是的,没错。(准确地说,开发工具的源代码)是表单
toString()
message
?但是我认为在
toString
中它调用了
消息
?是的,
Error.prototype.toString
打印错误构造函数的名称和消息属性。请参阅我关于如何覆盖
console.log
以获得所需行为的答案一个小问题是,为什么需要
再次恢复原始行为
您实际上并不需要它。如果你想恢复这种行为,它只是为了演示它是如何完成的。