在 node.js 中,在一个对象上调用 console.log,该对象有一个名为 inspect
的元素,打印出 undefined
,即使它仍然作为一个对象工作。我认为这是因为 Node 在内部使用检查来打印内容。
var thingTwo = {
num: 1,
func: function (x) { 2; },
inspect: function (x) { "hi"; },
};
console.log(thingTwo); // undefined
为避免将来出现此陷阱,是否有其他破坏标准功能的词的列表?
最佳答案
很酷,这激起了我的好奇心,事实上,有一个未记录的特性,对象可以提供它自己的 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
存在并且是触发行为的函数时。
关于javascript - 在 nodejs 中 'inspect' 就像一个保留字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11652454/