かっこいい、これは私の好奇心を刺激しました、そして確かに、オブジェクトがそれ自身の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
、動作をトリガーする関数です。