From fb383a0ad08470d2a50923c7f3baa3a59a530026 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 8 Sep 2012 15:09:59 -0700 Subject: [PATCH] util: make util.inspect() work when "hasOwnProperty" is overwritten --- lib/util.js | 8 ++++++-- test/simple/test-util-inspect.js | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/util.js b/lib/util.js index 53775e7..d9a5c2b 100644 --- a/lib/util.js +++ b/lib/util.js @@ -318,7 +318,7 @@ function formatError(value) { function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { var output = []; for (var i = 0, l = value.length; i < l; ++i) { - if (Object.prototype.hasOwnProperty.call(value, String(i))) { + if (hasOwnProperty(value, String(i))) { output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, String(i), true)); } else { @@ -349,7 +349,7 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { str = ctx.stylize('[Setter]', 'special'); } } - if (!visibleKeys.hasOwnProperty(key)) { + if (!hasOwnProperty(visibleKeys, key)) { name = '[' + key + ']'; } if (!str) { @@ -556,3 +556,7 @@ exports._extend = function(origin, add) { } return origin; }; + +function hasOwnProperty(obj, prop) { + return Object.prototype.hasOwnProperty.call(obj, prop); +} diff --git a/test/simple/test-util-inspect.js b/test/simple/test-util-inspect.js index 5b8fed9..b6f5bfa 100644 --- a/test/simple/test-util-inspect.js +++ b/test/simple/test-util-inspect.js @@ -107,3 +107,10 @@ assert.doesNotThrow(function() { // GH-2225 var x = { inspect: util.inspect }; assert.ok(util.inspect(x).indexOf('inspect') != -1); + +// an object with "hasOwnProperty" overwritten should not throw +assert.doesNotThrow(function() { + util.inspect({ + hasOwnProperty: null + }); +}); -- 2.7.4