Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

console: bind methods from the prototype chain in Console #24047

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-console-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down