Skip to content

Commit

Permalink
util: fix deprecated class prototype
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bengl authored and evanlucas committed Aug 20, 2016
1 parent 091ba2c commit 1f9fbad
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,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);

0 comments on commit 1f9fbad

Please sign in to comment.