Add Core.inherit()
authorMatthias Schmidt <gravatronics@live.com>
Wed, 23 Sep 2015 16:13:38 +0000 (18:13 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Wed, 23 Sep 2015 16:13:38 +0000 (18:13 +0200)
wcfsetup/install/files/js/WoltLab/WCF/Core.js

index da79e3743829e8a4ebb75ce0982bd74a3a5681fa..9ecf991c3b95f6d6d5ae6d59feaf502290956130 100644 (file)
@@ -113,6 +113,37 @@ define([], function() {
                        return newObj;
                },
                
+               /**
+                * Inherits the prototype methods from one constructor to another
+                * constructor.
+                * 
+                * @see https://github.com/nodejs/node/blob/7d14dd9b5e78faabb95d454a79faa513d0bbc2a5/lib/util.js#L697-L735
+                * @param       {function}      constructor             inheriting constructor function
+                * @param       {function}      superConstructor        inherited constructor function
+                * @param       {object=}       propertiesObject        additional prototype properties
+                */
+               inherit: function(constructor, superConstructor, propertiesObject) {
+                       if (constructor === undefined || constructor === null) {
+                               throw new TypeError("The constructor must not be undefined or null.");
+                       }
+                       if (superConstructor === undefined || superConstructor === null) {
+                               throw new TypeError("The super constructor must not be undefined or null.");
+                       }
+                       if (superConstructor.prototype === undefined) {
+                               throw new TypeError("The super constructor must have a prototype.");
+                       }
+                       
+                       constructor._super = superConstructor;
+                       constructor.prototype = Core.extend(Object.create(superConstructor.prototype, {
+                               constructor: {
+                                       configurable: true,
+                                       enumerable: false,
+                                       value: constructor,
+                                       writable: true,
+                               }
+                       }), propertiesObject || {});
+               },
+               
                /**
                 * Returns true if `obj` is an object literal.
                 *