console.log 格式错误对象不同于 Error.prototype.toString

console.log formats error object different from Error.prototype.toString

我正在查看 Error.prototype.toString 实施 here

它主要将 error.nameerror.message 打印为 name + ': ' + msg

但是当我将 Error 对象传递给 console.log 时,我看到 file not exists 错误将更多属性打印为 errno, code, syscall

console.log 调用什么来打印错误对象的字符串摘要?

代码:

var fs = require('fs')

fs.readFile('/abcd', 'utf8', function(err, res){
  console.log("the error toString method shows, " + err)
  console.log("the console log's string summary is,")
  console.log(err)
})

输出:

the error toString method shows, Error: ENOENT: no such file or directory, open '/abcd'
the console log's string summary is,
{ [Error: ENOENT: no such file or directory, open '/abcd'] errno: -2, code: 'ENOENT', syscall: 'open', path: '/abcd' }

console.log 未指定,因此每个浏览器实现不同的东西。 (和Node.js一样。)如果你想具体看什么,你可以看看浏览器源代码,当然IE/Edge除外。

在 Node.js 中,由于 require,我假设您正在使用它,代码是: