Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including monterey with all npm packages installed. Try it out:

var monterey = require("monterey")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

monterey v1.1.0

Minimal OOP for JavaScript

npm package build status dependency status code climate

Monterey is a tiny JavaScript library that adds simple but powerful classical inheritance capabilities to ES5 JavaScript.

Features

Although it may be used in a mostly-functional style, JavaScript has a simple and powerful object model at its heart built around prototypal inheritance. What this means in practice is that every function has a prototype object. When object instances are created using the new keyword in front of a function they reference all the properties and methods of that function's prototype. As such, all properties of the prototype object are also properties of that object.

Even though a function automatically comes with a prototype, a function may actually use any object as its prototype. Thus, we can mimic a classical inheritance strategy by setting the prototype of some function to an instance of some other function, its "superclass".

Monterey provides the following properties and methods that make it easier to use this pattern of inheritance in JavaScript:

  • Function#inherit(fn)
  • Function#extend([ properties ])
  • Function#isParentOf(fn)
  • Function#isChildOf(fn)
  • Function#isAncestorOf(fn)
  • Function#isDescendantOf(fn)
  • Function#parent
  • Function#ancestors

Function#extend is used to create an inheritance hierarchy that automatically sets up the prototype chain from one constructor function to another.

var Person = Object.extend({
  constructor: function (name) {
    this.name = name;
  },
  sayHello: function () {
    return 'Hi, my name is ' + this.name + '.';
  }
});

var Employee = Person.extend({
  constructor: function (name, title) {
    this.super(name);
    this.title = title;
  },
  sayHello: function () {
    return this.super() + " I'm an " + this.title + '!';
  }
});

var buzz = new Employee('Buzz', 'astronaut');
buzz.sayHello(); // Hi, my name is Buzz. I'm an astronaut!

Employee.parent; // Person
Employee.isChildOf(Person); // true
Person.isParentOf(Employee); // true

Employee.ancestors; // [ Person, Object ]
Employee.isDescendantOf(Object); //true
Object.isAncestorOf(Employee); // true

Under the hood Function#extend uses Function#inherit to setup the prototype chain.

Compatibility

Monterey works in any JavaScript environment that supports ES5, specifically the "static" methods of Object including Object.create, Object.defineProperty, and Object.defineProperties. Please see kangax's excellent ECMAScript 5 compatibility table for information on which browsers support ES5.

Installation

Using npm:

$ npm install monterey

Then, in your node program use require('monterey') to set it up.

In browsers, just include dist/monterey.min.js using a <script> tag.

<script src="monterey.min.js"></script>

Issues

Please file issues on the issue tracker on GitHub.

Tests

To run the tests in node:

$ npm install
$ npm test

To run the tests in Chrome:

$ npm install
$ npm run test-browser

License

MIT

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free