Design & Download
Your JS Library
in Seconds!!!

Snippet: inherit() - Basic Prototypal Inheritance

Written by Christopher West (cwest) on January 01, 2016.
Create sub-classes that inherit from a specified base case.
function inherit(subClass, baseClass, opt_prototype) {
  function SurrogateClass(){}
  SurrogateClass.prototype = baseClass.prototype;
  extend(subClass.prototype = new SurrogateClass(), { constructor: subClass, superClass: baseClass }, Object(opt_prototype));
  return subClass;
}

inherit(...) API Documentation

Create sub-classes that inherit from a specified base case.

Parameters

  1. subClass {Function}:
    The class that will inherit from the base class and whose prototype can optionally be extended.
  2. baseClass {Function}:
    The class from which subClass will inherit its prototype values. This will also be assigned as subClass.prototype.superClass.
  3. opt_prototype {Object}:
    Optional. If specified the properties will be assigned to subClass.prototype.superClass.

Returns

The sub-class passed in, but with the base class and the prototypes setup.