Skip to content

Commit 7cb5c65

Browse files
committed
[Fix] do not be fooled by a function’s own toString method
1 parent f74c82d commit 7cb5c65

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var hasWeakSet = typeof WeakSet === 'function' && WeakSet.prototype;
1212
var weakSetHas = hasWeakSet ? WeakSet.prototype.has : null;
1313
var booleanValueOf = Boolean.prototype.valueOf;
1414
var objectToString = Object.prototype.toString;
15+
var functionToString = Function.prototype.toString;
1516
var match = String.prototype.match;
1617
var bigIntValueOf = typeof BigInt === 'function' ? BigInt.prototype.valueOf : null;
1718

@@ -181,7 +182,7 @@ function toStr(obj) {
181182

182183
function nameOf(f) {
183184
if (f.name) { return f.name; }
184-
var m = match.call(f, /^function\s*([\w$]+)/);
185+
var m = match.call(functionToString.call(f), /^function\s*([\w$]+)/);
185186
if (m) { return m[1]; }
186187
return null;
187188
}

test/fn.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test('function name', function (t) {
1414
}());
1515
f.toString = function () { return 'function xxx () {}'; };
1616
var obj = [1, 2, f, 4];
17-
t.equal(inspect(obj), '[ 1, 2, [Function: xxx], 4 ]');
17+
t.equal(inspect(obj), '[ 1, 2, [Function (anonymous)], 4 ]');
1818
});
1919

2020
test('anon function', function (t) {

0 commit comments

Comments
 (0)