Skip to content

Commit 1f9fbad

Browse files
benglevanlucas
authored andcommitted
util: fix deprecated class prototype
Ensure the wrapped class prototype is exactly the unwrapped class prototype, rather than an object whose prototype is the unwrapped class prototype. This ensures that instances of the unwrapped class are instances of the wrapped class. This is useful when both a wrapped class and a factory for the unwrapped class are both exposed. Ref: #8103 PR-URL: #8105 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 091ba2c commit 1f9fbad

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/internal/util.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ exports._deprecate = function(fn, msg) {
6666
// The wrapper will keep the same prototype as fn to maintain prototype chain
6767
Object.setPrototypeOf(deprecated, fn);
6868
if (fn.prototype) {
69-
Object.setPrototypeOf(deprecated.prototype, fn.prototype);
69+
// Setting this (rather than using Object.setPrototype, as above) ensures
70+
// that calling the unwrapped constructor gives an instanceof the wrapped
71+
// constructor.
72+
deprecated.prototype = fn.prototype;
7073
}
7174

7275
return deprecated;

test/fixtures/deprecated-userland-class.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ class deprecatedClass {
77
const deprecated = util.deprecate(deprecatedClass, 'deprecatedClass is deprecated.');
88

99
const instance = new deprecated();
10+
const deprecatedInstance = new deprecatedClass();
1011

1112
assert(instance instanceof deprecated);
1213
assert(instance instanceof deprecatedClass);
14+
assert(deprecatedInstance instanceof deprecated);
15+
assert(deprecatedInstance instanceof deprecatedClass);

0 commit comments

Comments
 (0)