错误记录关联数组属性是使用' util. '的函数.在node.js中检查

Error logging associative array properties that are functions using `util.inspect` in node.js

本文关键字:函数 node js 检查 util 记录 属性 错误 关联 数组      更新时间:2023-09-26

我使用node.js的util.inspect调用转储JavaScript关联数组到日志。所讨论的关联数组包括作为函数的成员属性。例如:

var pendingscreen = {};
pendingscreen['timeoutfunction'] = function(){ sendmsg(); };
pendingscreen['timeout'] = setTimeout(pendingscreen['timeoutfunction'], 1000);
console.log(util.inspect(pendingscreen));

当我运行这个,我得到这个错误:

TypeError: Function.prototype.toString is not generic
    at Client.toString (native)
    at String.STRING_ADD_LEFT (native)
    at isRegExp (util.js:287:14)
    at format (util.js:184:11)
    at util.js:216:19
    at Array.map (native)
    at format (util.js:193:23)
    at util.js:216:19
    at Array.map (native)
    at format (util.js:193:23)

是否有方法检查关联数组的成员,其中一些成员可能是函数?

util.inspect应该可以很好地处理这个问题。但是,你正在使用的node.js版本中有一个错误,可能会导致此错误。

在新版本(>=0.4.11)中修复。

在我看来这实际上像是Node中的一个bug。

正常情况下,当你检查对象时,它只会打印出"[Function]"。

编辑:我不确定这实际上是问题了。触发此错误的代码如下:
(function(){ }).toString.call(null)

用非函数调用函数'toString'。我不知道这是怎么发生的