Skip to content

Commit

Permalink
util: set super_ property to non-enumerable
Browse files Browse the repository at this point in the history
Using `util.inherits()` adds a `super_` property to the constructor
and inspecting that constructor is going to show that property even
though it's not useful for the user.

Therefore the property is now set as non-enumerable and such entries
are not visible by default when using `console.log()` or
`util.inspect()`.

PR-URL: nodejs#23107
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
BridgeAR committed Oct 1, 2018
1 parent 6df2c55 commit 5e6940d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,11 @@ function inherits(ctor, superCtor) {
throw new ERR_INVALID_ARG_TYPE('superCtor.prototype',
'Function', superCtor.prototype);
}
ctor.super_ = superCtor;
Object.defineProperty(ctor, 'super_', {
value: superCtor,
writable: true,
configurable: true
});
Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
}

Expand Down
10 changes: 9 additions & 1 deletion test/parallel/test-util-inherits.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ function B(value) {
inherits(B, A);
B.prototype.b = function() { return this._b; };

assert.strictEqual(B.super_, A);
assert.deepStrictEqual(
Object.getOwnPropertyDescriptor(B, 'super_'),
{
value: A,
enumerable: false,
configurable: true,
writable: true
}
);

const b = new B('b');
assert.strictEqual(b.a(), 'a');
Expand Down

0 comments on commit 5e6940d

Please sign in to comment.