diff options
author | vladimir <vlad2t@hotmail.com> | 2016-07-12 23:09:12 +0200 |
---|---|---|
committer | cjihrig <cjihrig@gmail.com> | 2016-08-04 11:44:40 -0400 |
commit | 320f433dcdb411da22d8ee9427bbdf5b071faca2 (patch) | |
tree | 859f5a07381e755da9b986452f087048e4943ebe /lib/internal/util.js | |
parent | e1643ccc5a5ecf7cb779472d244459469c9971a1 (diff) | |
download | android-node-v8-320f433dcdb411da22d8ee9427bbdf5b071faca2.tar.gz android-node-v8-320f433dcdb411da22d8ee9427bbdf5b071faca2.tar.bz2 android-node-v8-320f433dcdb411da22d8ee9427bbdf5b071faca2.zip |
util: support classes in util.deprecate()
Classes cannot be instantiated without new, but util.deprecate()
uses Function.prototype.apply(). This commit uses new.target to
detect constructor calls, allowing classes to be deprecated.
PR-URL: https://github.com/nodejs/node/pull/7690
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michaƫl Zasso <mic.besace@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/internal/util.js')
-rw-r--r-- | lib/internal/util.js | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/lib/internal/util.js b/lib/internal/util.js index e3d9a66b3b..073879d9e9 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -60,9 +60,18 @@ exports._deprecate = function(fn, msg) { var warned = false; function deprecated() { warned = exports.printDeprecationMessage(msg, warned, deprecated); + if (new.target) { + return Reflect.construct(fn, arguments, new.target); + } return fn.apply(this, arguments); } + // 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); + } + return deprecated; }; |