Skip to content
Browse files

console: bind methods from the prototype chain in Console

In 6223236 we made the console.Console function construct an object
with methods from Console.prototype bound to the instance, instead of
with methods found on the prototype chain to be bound on the instances,
thus breaking users overriding these methods when subclassing Console
because they are not expecting this function to construct a
namespace whose methods are not looked up from the prototype

This patch restores the previous behavior since it does not affect
the characteristics of the global console anyway. So after this patch,
the Console function still constructs normal objects with function
properties looked up from the prototype chain but bound to the
instances always.

PR-URL: #24047
Reviewed-By: John-David Dalton <>
Reviewed-By: Luigi Pinca <>
Reviewed-By: Colin Ihrig <>
  • Loading branch information...
joyeecheung authored and BridgeAR committed Nov 2, 2018
1 parent b2b0645 commit 7f5bb9d3bf29be8ce65becf4db6ceab78cdd48f3
Showing with 10 additions and 1 deletion.
  1. +4 −1 lib/console.js
  2. +6 −0 test/parallel/test-console-instance.js
@@ -132,7 +132,10 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
var keys = Object.keys(Console.prototype);
for (var v = 0; v < keys.length; v++) {
var k = keys[v];
this[k] = Console.prototype[k].bind(this);
// We have to bind the methods grabbed from the instance instead of from
// the prototype so that users extending the Console can override them
// from the prototype chain of the subclass.
this[k] = this[k].bind(this);

@@ -105,10 +105,16 @@ out.write = err.write = (d) => {};
class MyConsole extends Console {
hello() {}
// See if the methods on Console.prototype are overridable.
log() { return 'overridden'; }
const myConsole = new MyConsole(process.stdout);
assert.strictEqual(typeof myConsole.hello, 'function');
assert.ok(myConsole instanceof Console);
assert.strictEqual(myConsole.log(), 'overridden');

const log = myConsole.log;
assert.strictEqual(log(), 'overridden');

// Instance that does not ignore the stream errors.

0 comments on commit 7f5bb9d

Please sign in to comment.
You can’t perform that action at this time.