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

extendable.js

v0.0.1

Published

A Node.js, AMD module, or global module which assists in extending objects and overriding object methods.

Downloads

2

Readme

#Extendable.js

A Node.js, AMD module, or global module which assists in extending objects and overriding object methods using prototypal inheritance.

#Usage

##Node.js

    var Extendable = require('extendable.js');

##AMD require

require(['extendable.js'], function(Extendable) {
    //
});

or you can use it as a global variable window.Extendable in your browser.

#Examples

##Extension of object literals

###extend()

extend(props) creates a new object, set the current object (this) as the object's prototype and copies the first-level properties of props to the new object.

var props = {
    a: 1,
    b: {
      c: 3
    }
};
var extended = Extendable.extend(props);

Object extended has properties a and b copied from props and it's prototype is Extendable:

extended.a === props.a;             // returns true
extended.b === props.b;             // returns true
Extendable.isPrototypeOf(extended); // returns true

###create()

Acts the same as if the extend() was called without any arguments, returns the same as calling Object.create(this).

###override()

override(methodName, newMethod) overrides an existing method methodName in the object and adds the ability to call the overridden method from the new method:

var obj1 = Extendable.extend({
    value: 5,
    calculate: function(a) {
        return this.value + a;
    }
});

var obj2 = obj1.extend().override('calculate', function(p_super, a, b) {
    return p_super(a) + b;
});

obj1.calculate(3);     // returns 8
obj2.calculate(3, 10); // returns 18

Additional combinations are possible:

var original = {
    a: 1,
    b: 2,
    sum: function(c, d) {
        return this.a + this.b + c + d;
    }
};

var extended = Extendable.extend(original)
    // override the sum method
    .override('sum', function(p_super, c, d) {
        var value = p_super(c, d);
        return value * 2;
    })
    // extend the object with the overridden method and modify the
    // variable which is used in the overridden method.
    .extend({
        b: 5
    });

original.sum(3, 4); // 1 + 2 + 3 + 4 = 10
extended.sum(3, 4); // (1 + 5 + 3 + 4) * 2 = 26

##Constructor extension


// Person.js AMD module
define(['Extendable'], function(Extendable) {
    
    function Person(name) {
        this.name = name;
    }
    
    var PersonPrototype = {
        setJob: function(job) {
            this.job = job;
        },
        setAge: function(age) {
            this.age = age;
        }
    };
    
    return Extendable.extend(Person, PersonPrototype);
    
});

// Plumber.js AMD module
define(['Person'], function(Person) {
    
    function Plumber(name, tools) {
        this.superclass.call(this, name);
        
        this.setJob('plumber');
        this.setAge(25);
        this.setTools(tools);
    }
    
    var PlumberPrototype = {
        setTools: function(tools) {
            this.tools = tools;
        }
    };
    
    return Person.extend(Plumber, PlumberPrototype);
});

// index.js
require(['Plumber', 'Person'], function(Plumber, Person) {

    var joe = new Plumber('joe johnson', 'wrench');
    
    // joe instanceof Plumber returns true
    // joe instanceof Person returns true
    // Person.prototype.isPrototypeOf(joe) returns true
    // Plumber.prototype.isPrototypeOf(joe) returns true
    
    // joe.name is 'joe johnson'
    // joe.age is 25
    // joe.job is 'plumber'
    // joe.tools is 'wrench'

});

#License

MIT