【问题标题】:Error logging associative array properties that are functions using `util.inspect` in node.js错误记录关联数组属性,这些属性是在 node.js 中使用 `util.inspect` 的函数
【发布时间】:2011-09-07 03:37:34
【问题描述】:

我正在使用 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)

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

【问题讨论】:

  • 您能否向我确认这不是由您的 sendmsg() 函数中的某些问题引起的问题?只需执行“pendingscreen['timeoutfunction'] = function(){};”。
  • 不是:我之前将其更改为匿名函数,但它仍然崩溃。我放弃了,只是重组了我的代码,所以我不需要使用util.inspect

标签: javascript node.js associative-array


【解决方案1】:

util.inspect 应该可以很好地处理这个问题。但是,您使用的 bug in the node.js 版本可能会导致此错误。

此问题已在较新版本 (>=0.4.11) 中得到修复。

【讨论】:

    【解决方案2】:

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

    通常,当您检查对象时,它只会打印出“[Function]”。

    编辑:我不确定这是否真的是问题所在。 这个错误是由这样的事情触发的:

    (function(){ }).toString.call(null)
    

    用不是函数的东西调用函数“toString”。我不知道这会如何发生。

    【讨论】:

    • 有趣:0.4.11 是报告的内容。
    • 我不知道该告诉你什么。我刚刚尝试将您的示例代码粘贴到节点 0.4.11 终端中,它工作正常。不过还是试试新版本吧。
    猜你喜欢
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    • 2012-01-17
    • 2020-04-01
    相关资源
    最近更新 更多