Nodejs Class Extend subclass(base)

Here you can find the source of subclass(base)

Method Source Code

Function.prototype.subclass = function(base) {
  var c = Function.prototype.subclass.nonconstructor;
  c.prototype = base.prototype;
  this.prototype = new c();/* ww w . j  a v  a  2 s. co  m*/
};
Function.prototype.subclass.nonconstructor = function() {};

// Get the size of an object
Object.size = function(obj) {
  var size = 0, key;
  for (key in obj) {
    if (obj.hasOwnProperty(key)) size++;
  }
  return size;
};

function arrayContains(arr, obj) {
  for (var i = 0; i < arr.length; ++i) {
    if (arr[i] === obj) {
      return true;
    }
  }
  return false;
};

function arrayRemove(arr, obj) {
  for (var i = 0; i < arr.length; ++i) {
    if (arr[i] === obj) {
      arr.splice(i, 1);
    }
  }
}

function arrayRandomElement(arr) {
  return arr[Math.floor(Math.random() * arr.length)];
}

String.prototype.lpad = function(padString, length) {
  var str = this;
  while (str.length < length)
    str = padString + str;
  return str;
}

Related

  1. inherits(BaseClass)
    Function.prototype.inherits = function (BaseClass) {
      function Surrogate () {}
      Surrogate.prototype = BaseClass.prototype;
      this.prototype = new Surrogate();
      this.prototype.constructor = this;
    };
    String.prototype.capitalize = function() {
        return this.charAt(0).toUpperCase() + this.slice(1);
    };
    ...
    
  2. inheritsFrom(parent)
    Function.prototype.inheritsFrom = function(parent){ 
      this.prototype = new parent();
      this.prototype.constructor = this;
      this.prototype.parent = parent.prototype;
      return this;
    };
    
  3. Inherits(parent)
    Function.prototype.Inherits = function(parent) {
      this.prototype = new parent();
      this.prototype.constructor = this;
      this.prototype.parent = parent.prototype;
    };
    
  4. inherit(ParentClass)
    Function.prototype.inherit = function(ParentClass) {
      this.prototype = Object.create(ParentClass.prototype);
      this.prototype.constructor = this;
      this.prototype.parent = ParentClass.prototype;