sys.inherts to shadow constructor property from enumerability
authorDmitry Baranovskiy <Dmitry@Baranovskiy.com>
Mon, 28 Jun 2010 01:05:22 +0000 (11:05 +1000)
committerRyan Dahl <ry@tinyclouds.org>
Wed, 30 Jun 2010 02:11:40 +0000 (19:11 -0700)
thanks to ES5 features in V8 it is quite easily possible.

lib/sys.js

index f877dad..ac8b023 100644 (file)
@@ -315,9 +315,11 @@ exports.pump = function (readStream, writeStream, callback) {
  * @param {function} superCtor Constructor function to inherit prototype from
  */
 exports.inherits = function (ctor, superCtor) {
-  var tempCtor = function(){};
-  tempCtor.prototype = superCtor.prototype;
-  ctor.super_ = superCtor;
-  ctor.prototype = new tempCtor();
-  ctor.prototype.constructor = ctor;
+    ctor.super_ = superCtor;
+    ctor.prototype = Object.create(superCtor.prototype, {
+        constructor: {
+            value: ctor,
+            enumerable: false
+        }
+    });
 };