Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1f9fbad

Browse filesBrowse 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
Copy full SHA for 1f9fbad

File tree

Expand file treeCollapse file tree

2 files changed

+7
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+7
-1
lines changed
Open diff view settings
Collapse file

‎lib/internal/util.js‎

Copy file name to clipboardExpand all lines: lib/internal/util.js
+4-1Lines 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;
Collapse file

‎test/fixtures/deprecated-userland-class.js‎

Copy file name to clipboardExpand all lines: test/fixtures/deprecated-userland-class.js
+3Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.