Function.inherits >= 0.2.0

Purpose

Make one class inherit from the other

Syntax

Function.inherits ( Stringparent_class Stringnamespace Functionnew_constructor Booleando_constitutors= true );

Parameters

parent_class
String, function or array
namespace
The namespace to store the new class in
new_constructor
The actual constructor
do_constitutors
Do the constitutors

Return values

Function

Examples

Create a new class and add methods

This is a very basic example of creating a new class. Protoblast always keeps a record of all the created classes.

We'll use it as a basis for later examples.

var Animal = Function.inherits(function Animal(species, noise) {
    this.species = species;
    this.noise = noise;
});>>> Function

Animal.setMethod(function makeNoise() {
    return this.noise;
});>>> Function

var dog = new Animal('dog', 'bark');
dog.makeNoise();>>> 'bark'

Inherit from the Animal class by referring to its name


var Cat = Function.inherits('Animal', function Cat() {
    // The constructor gets a property called `super`
    // which refers to the constructor of `Animal`
    Cat.super.call(this, 'cat', 'meow');
});>>> Function

var cat = new Cat();
cat.makeNoise();>>> 'meow'

Inherit from a class before it exists


// Inherit from a class that does not exist yet
// It'll return the constructor already, to which you can already add methods
Function.inherits('MyFutureClass', function InheritedClass() {});>>> Function

// Create the actual class
Function.inherits(function MyFutureClass() {});