酷,这激起了我的好奇心,事实上,有一个未记录的特性,一个对象可以提供它自己的inspect
方法。util.js中的相关代码:
function formatValue(ctx, value, recurseTimes) {
// Provide a hook for user-specified inspect functions.
// Check that value is an object with an inspect function on it
if (value && typeof value.inspect === 'function' &&
// Filter out the util module, it's inspect function is special
value.inspect !== exports.inspect &&
// Also filter out any prototype objects using the circular check.
!(value.constructor && value.constructor.prototype === value)) {
return String(value.inspect(recurseTimes));
}
即,它仅在inspect
存在并且是触发行为的函数时。