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 sand-extend with all npm packages installed. Try it out:

var sandExtend = require("sand-extend")

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

sand-extend v0.0.5

Class Inheritance made simple

Build Status npm version

sand-extend

sand-extend offers a simple way to have class inheritance in javascript. It works well with and without the sand framework.

Currently sand-extend offers 2 ways to do inheritance:

  1. Class Based method with construct (Full inheritance)
  2. Extend Based method with prototype inheritance (has some caveats)

Class Based

Class based inheritance is a little different then what you are used to but offers full inheritance concept

var Animal = Class.extend({
  // This is the constructor method
  construct: function(name) {
    this.name = name || 'No Name';
  },

  speak: function() {
    return 'My name is ' + this.name;
  }
});

var Dog = Animal.extend({
  // Just calling super, could be omitted
  construct: function(name) {
    this.super(name);
  },

  speak: function() {
    return this.super() + ' and ' + 'I Bark';
  }
});

var Collie = Dog.extend({
  // Auto calls parent constructor
  
  speak: function() {
    return this.super() + ' and ' + 'I am a Collie';
  }
});

Extend Based

Extend based uses the typical prototype inheritance but adds the super method so you can call the parent.

Example

require('sand-extend').enableGlobalExtend();

function Animal(name) {
  this.name = name || 'No Name';
}

Animal.prototype.speak = function() {
  return 'My name is ' + this.name;
};

function Dog(name) {
  this.super(name);
}

// Extend the Animal class
Dog.extend(Animal, {
  // Override the speak class and 
  // call the parent method
  speak: function() {
    return this.super() + ' and ' + 'I Bark';
  }
});

function Cat(name) {
  this.super(name);
}

// Extend the Animal class
Cat.extend(Animal, {
  // Override the speak class and 
  // call the parent method
  speak: function() {
    return this.super() + ' and ' + 'I Meow';
  }
});

var dog = new Dog('Terrie');
var cat = new Cat('Hana');

// Prints out `My Name is Terrie and I Bark`
console.log(dog.speak());

// Prints out `My Name is Hana and I Meow`
console.log(cat.speak());

Caveats

Extend supports multiple levels of inheritance but you can't call this.super() in all constructors as it has an infinite loop issue. this.super() can still be called in all methods though, an infinite number deep.

OMG It Extends Function?

Yes, yes it does, with a single getter extend, and no it won't break your code, because it does this properly with a non-enumerable property. Also it only does this if you enable it with enableGlobalExtend. It is off by default.

To begin

  1. Install it:

    $ npm install sand-extend -S
    
  2. Require it and use:

    // Either enable Global extend
    require('sand-extend').enableGlobalExtend();
    
    // or Use extend by itself
    var Extend = require('sand-extend').Extend;
    

Tests

To run the tests for sand-extend simply run:

$ npm test

License

ISC © 2014 Pocketly

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