npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sand-extend

v0.0.5

Published

Class Inheritance made simple

Downloads

12

Readme

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