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

util: fix deprecated class prototype #8105

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/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ exports._deprecate = function(fn, msg) {
// The wrapper will keep the same prototype as fn to maintain prototype chain
Object.setPrototypeOf(deprecated, fn);
if (fn.prototype) {
Object.setPrototypeOf(deprecated.prototype, fn.prototype);
// Setting this (rather than using Object.setPrototype, as above) ensures
// that calling the unwrapped constructor gives an instanceof the wrapped
// constructor.
deprecated.prototype = fn.prototype;
}

return deprecated;
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/deprecated-userland-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class deprecatedClass {
const deprecated = util.deprecate(deprecatedClass, 'deprecatedClass is deprecated.');

const instance = new deprecated();
const deprecatedInstance = new deprecatedClass();

assert(instance instanceof deprecated);
assert(instance instanceof deprecatedClass);
assert(deprecatedInstance instanceof deprecated);
assert(deprecatedInstance instanceof deprecatedClass);