node打印对象的2种方法
 发布于 11 年前  作者 i5ting  23702 次预览  最后一次回复是 11 年前  来自 分享 

法1

console.dir(response)

法2

var inspect = require('util').inspect;
console.log(inspect(response))

目前我测试是差不多的,不知道它们有啥差别,求指教

4 回复
MiguelValentine

Console.prototype.dir = function(object, options) { this._stdout.write(util.inspect(object, util._extend({ customInspect: false }, options)) + ‘\n’); };

Console.prototype.log = function() { this._stdout.write(util.format.apply(this, arguments) + ‘\n’); };

i5ting

@MiguelValentine 谢谢阿,瞬间就懂了

alsotang

@MiguelValentine @i5ting 看来还是看源码的牛逼啊。。 util.inspect 还有个

showHidden - if true then the object’s non-enumerable properties will be shown too. Defaults to false.

选项。 有时候会有用。