diff --git a/lib/avltree.js b/lib/avltree.js index 10b2b82..c821c2f 100644 --- a/lib/avltree.js +++ b/lib/avltree.js @@ -3,7 +3,6 @@ */ var BinarySearchTree = require('./bst') , customUtils = require('./customUtils') - , util = require('util') , _ = require('underscore') ; @@ -46,7 +45,7 @@ function _AVLTree (options) { /** * Inherit basic functions from the basic binary search tree */ -util.inherits(_AVLTree, BinarySearchTree); +customUtils.inherits(_AVLTree, BinarySearchTree); /** * Keep a pointer to the internal tree constructor for testing purposes diff --git a/lib/customUtils.js b/lib/customUtils.js index 611b52f..bc30496 100644 --- a/lib/customUtils.js +++ b/lib/customUtils.js @@ -39,3 +39,32 @@ function defaultCheckValueEquality (a, b) { return a === b; } module.exports.defaultCheckValueEquality = defaultCheckValueEquality; + +/** + * Inherit the prototype methods from one constructor into another. + * + * The Function.prototype.inherits from lang.js rewritten as a standalone + * function (not on Function.prototype). NOTE: If this file is to be loaded + * during bootstrapping this function needs to be rewritten using some native + * functions as prototype setup using normal JavaScript does not work as + * expected during bootstrapping (see mirror.js in r114903). + * + * @param {function} ctor Constructor function which needs to inherit the + * prototype. + * @param {function} superCtor Constructor function to inherit prototype from. + * @throws {TypeError} Will error if either constructor is null, or if + * the super constructor lacks a prototype. + */ +function inherits(ctor, superCtor) { + if (ctor === undefined || ctor === null) + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'ctor', 'function'); + if (superCtor === undefined || superCtor === null) + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'superCtor', 'function'); + if (superCtor.prototype === undefined) { + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'superCtor.prototype', + 'function'); + } + ctor.super_ = superCtor; + Object.setPrototypeOf(ctor.prototype, superCtor.prototype); +} +module.exports.inherits = inherits;