开发者

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

I'm using node.js's util.inspect call to dump a JavaScript associative array to the log. The associative array in question includes member properties that are functions. An example is:

var pendingscreen = {};
pendingscreen['timeoutfunction'] = function(){ sendmsg(); };
pendingscreen['timeout'] = setTim开发者_运维技巧eout(pendingscreen['timeoutfunction'], 1000);
console.log(util.inspect(pendingscreen));

When I run this, I get this error:

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)

Is there any way to inspect the members of an associative array where some of the members could be functions?


util.inspect should handle this just fine. However there is a bug in the node.js version you are using which can cause this error.

This is fixed in newer version (>=0.4.11).


That actually looks like a bug in Node to me.

Normally it would just print out "[Function]" when you inspect the object.

Edit: I'm not sure this is actually the issue anymore. This error is triggered by something like this:

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

Calling the Function 'toString' with something that is not a function. I have no idea how that would happen though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜