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

objex

v0.4.1

Published

Easy prototype and static methods inheritance via inheritedBy method

Downloads

1,892

Readme

Objex NPM version Build Status

Dependency status Development Dependency status

Base constructor to ease prototype inheritance.

Skip to API.

Simple example

var Objex = require('objex'),

    Animal = Objex.create(function() {
        Animal.__super.apply(this, arguments);
        this.feet = 4;
        this.hands = 0;
        this.head = 1;
    }),

    Wolf = Animal.create(),

    Dog = Wolf.create(),

    Monkey = Animal.create(function() {
        Monkey.__super.apply(this, arguments);

        this.feet = 2;
        this.hands = 2;
    }),

    wolf = new Wolf(),
    dog = new Dog(),
    monkey = new Monkey();

// check inheritance
console.log('wolf is instance of Wolf', wolf instanceof Wolf);
console.log('wolf is instance of Animal', wolf instanceof Animal);

console.log('monkey is instance of Monkey', monkey instanceof Monkey);
console.log('monkey is instance of Animal', monkey instanceof Animal);

console.log('dog is instance of Dog', dog instanceof Dog);
console.log('dog is instance of Wolf', dog instanceof Wolf);
console.log('dog is instance of Animal', dog instanceof Animal);

Don't copy / partially copy static properties of super-class

var Animal = Objex.create(function() {
        Animal.__super.apply(this, arguments);
        Animal.count++;
    }),
    Sheep,
    Wolf;

Animal.count = 0;

Animal.kill = function() {
    if (Animal.count > 0) {
        Animal.count--;
    }
};

// pass `false` as first argument to prevent static fields bypass,
// static method `create` will be copied anyway.
// Assume, you want to count sheeps separately.
// Animal.count will not be copied to Sheep.count, because it useless here.
// Animal.kill will not be copied to Sheep too, aren't you want to shoot your own Sheep?
Sheep = Animal.create(false, function() {
    Sheep.count++;
});

Sheep.count = 0;

// pass array of property names as first argument
// to copy certain static properties only.
// Static property `count` is useless for Wolf,
// but you are still able to kill it, and decrease global Animal.count!
Wolf = Animal.create(['kill'], function() {
    Wolf.__super.apply(this, arguments);
    this.hungry = false;
});

Set base constructor which differs from the Object

Suppose, you need to create Error inheritor, so your prototype chain must look like ErrorEx > Error > Object, not ErrorEx > Object.

Objex.wrap at your service!

var MyOwnErrorEx;

function ErrorEx() {
    ErrorEx.__super.apply(this, arguments);

    this.extended = true;
}

util.inherits(ErrorEx, Error);
Objex.wrap(ErrorEx, Error);
// now ErrorEx has `create` method and `__super` property

MyOwnErrorEx = ErrorEx.create(function(code) {
    this.code = code;
    MyOwnErrorEx.__super.apply(this, Array.prototype.slice.call(arguments, 1));
});

Mixin

Mixin (copy) static and prototype properties from any constructor to the Objex successor, without prototype chain modification. Mixed properties will be own properties of a target object.

var Animal = Objex.create(),
    Dog = Animal.create(),
    TrickyPet = function() {}, // mixin ctor
    LoudVoice = function() {}, // more one
    jimmy = new Dog();

Animal.prototype.say = function(wat) {
    console.log(wat);
}

// mixin method
TrickyPet.prototype.jumpBackward = function() {
    this.say('Woo-oo!');
};

// copy TrickyPet.prototype methods to Dog.prototype
Dog.mixin(TrickyPet);

// call copied method from the instance of Dog
jimmy.jumpBackward();

// mixin method
LoudVoice.prototype.say = function(wat) {
    console.log('(loud voice)', wat);
};

// override existing Dog prototype's method `say`
Dog.mixin({ override : true }, LoudVoice);

jimmy.jumpBackward();

Dynamic mixing

You can declare special static method __objexOnMixing in the mixin, which will be called while mixin applies:

var Human = Objex.create(function() {
        Human.__super.apply(this, arguments);
        Human.population++;
    }),
    Mutant = function() {}; // mixin

Human.population = 1000;

Human.prototype.doSomething = function() {
    console.log('Doing something...');
};

Mutant.__objexOnMixing = function(Base, isHungry) {
    var doSomething = Base.prototype.doSomething;

    if (isHungry && typeof doSomething === 'function') {
        // extend Base method if Mutant is hungry
        Base.prototype.doSomething = function() {
            // eat anybody before doing something
            Base.population--;
            return doSomething.apply(this, arguments);
        }
    }
};

// people became hungry mutants
Human.mixin(Mutant, true);

var human = new Human();

console.log(Human.population); // -> 1001

human.doSomething(); // -> 'Doing something...'

console.log(Human.population); // -> 1000

API

Description of the Objex and its successors static methods.

create([options], ctor)

Create successor of the callee with constructor passed as ctor argument. Argument options describes how the successor inherits statis fields:

  • true – default value; copy all static properties w/o existing properties overriding;
  • false – don't copy statis properties;
  • { include : [], exclude : [] } – object with optional fields include and exclude which are arrays of properties' names to copy of not;
  • Array of String – shotcut syntax for { include : [] }.

wrap(ctor)

Add Objex create and mixin static methods to the ctor which is not Objex successor without prototype chain modification.

mixin([options], ctor)

Mixin (copy) static and prototype methods of ctor to the callee contructor and its prototype if they doesn't exists.

Argument options is mostly the same as of the create method, but the object argument can contain additional properties:

  • {Boolean} [override=false] — if it equals true existing methods of the callee will be overriden by mixin's methods,
  • {Boolean} [skipDynamicMixing=false] — if it equals true __objexOnMixing won’t be called.