summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2018-11-03 03:20:23 +0800
committerRich Trott <rtrott@gmail.com>2018-11-05 20:53:59 -0800
commit04a291abec3b86ed902c5e9919b2a8adf1e07d14 (patch)
tree7ad5a335b20ea7aa12a5dcefb1d53c7f5c6884ba
parentc3b674cc116e6656a0eedd9e8f01b1c01c955f2c (diff)
downloadandroid-node-v8-04a291abec3b86ed902c5e9919b2a8adf1e07d14.tar.gz
android-node-v8-04a291abec3b86ed902c5e9919b2a8adf1e07d14.tar.bz2
android-node-v8-04a291abec3b86ed902c5e9919b2a8adf1e07d14.zip
console: bind methods from the prototype chain in Console
In 62232361 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 chain. 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: https://github.com/nodejs/node/pull/24047 Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
-rw-r--r--lib/console.js5
-rw-r--r--test/parallel/test-console-instance.js6
2 files changed, 10 insertions, 1 deletions
diff --git a/lib/console.js b/lib/console.js
index 2e56f7bbba..39e55c202c 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 87708808a8..e6e1e0db24 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.