mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
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: https://github.com/nodejs/node/pull/8103 PR-URL: https://github.com/nodejs/node/pull/8105 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
3177b99737
commit
249bb8da2f
@ -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;
|
||||
|
3
test/fixtures/deprecated-userland-class.js
vendored
3
test/fixtures/deprecated-userland-class.js
vendored
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user