Skip to main content
100%

inherit.js

✓ Published0🌍 Public
Aabernier
Last edited Mar 13, 2015
Created on Mar 13, 2015

This example demonstrates classical inheritance for JavaScript constructors by providing an `inherit` utility function. The `inherit.js` file defines the helper, which copies a parent's static properties and sets up prototype chaining while exposing the parent's prototype as `child.uber`. The accompanying `index.html` shows a concrete usage where `Snake` inherits from `Animal`, calling the parent constructor via `Snake.uber.constructor.apply` and overriding the `move` method to invoke the parent's implementation. The example relies entirely on JavaScript's native prototype system, with no external libraries.

AI-generated description

Classical inheritance for constructors.

INSTALL

Client-side

<script src="inherit.js"></script>

Server-side

npm install https://gist.github.com/abernier/decf360c677a54aa415c/download
var inherit = require('inherit');

Usage

function A() {}
A.prototype.foo = function () {};

function B() {
  B.uber.constructor.apply(this, arguments);
}
inherit(B, A);
B.prototype.foo = function () {
  this.constructor.uber.foo.apply(this, arguments);
}

NB:

  • B.uber references to A.prototype
  • classically, you can access the ctor from prototype's methods thanks to constructor property.
ISC Licensed

Similar vizzes