Nimble Polyglot Microcosm
Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »

sand-extend

0.0.5 • Public • Published

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

install

npm i sand-extend

Downloadsweekly downloads

237

version

0.0.5

license

ISC

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
  • avatar
  • avatar
Report a vulnerability