我看到 Error.prototype.toString 实现 here .
它主要将 error.name
和 error.message
打印为 name + ': ' + msg
。
但是当我将 Error
对象传递给 console.log
时,我看到 文件不存在
错误将更多属性打印为 errno、代码、系统调用
等
console.log 调用什么来打印 Error 对象的字符串摘要?
代码:
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
而使用 Node.js,代码是:
关于javascript - console.log 格式化错误对象与 Error.prototype.toString 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34099996/