diff --git a/lib/console.js b/lib/console.js index 2e56f7bbba5ad8..39e55c202c4f4b 100644 --- a/lib/console.js +++ b/lib/console.js @@ -129,7 +129,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); } } diff --git a/test/parallel/test-console-instance.js b/test/parallel/test-console-instance.js index 87708808a8652c..e6e1e0db24d23c 100644 --- a/test/parallel/test-console-instance.js +++ b/test/parallel/test-console-instance.js @@ -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.