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

selfish

v1.1.0

Published

Class-free, pure prototypal multiple inheritance

Downloads

91

Readme

selfish

Build Status

Class-free, pure prototypal multiple inheritance that lets you write expressive, well-structured code.

Install

server-side

npm install selfish

client-side

bower install selfish

Require

server-side

var Base = require('!raw.github.com/Gozala/selfish/v1.0.0/selfish').Base

client-side RequireJS

define(['path/to/selfish'], function(selfish) {
   var Base = selfish.Base;
})

Examples

Basics

// Instead of creating classes, you create prototype objects. Let's look
// at this simple example first:
var Dog = Base.extend({
  bark: function() {
    return 'Ruff! Ruff!'
  }
})

// Forget about classes, javascript is a prototypal language:
typeof Dog                // object

// Use the new operator to create an instance:
var dog = new Dog()
dog.bark()                // 'Ruff! Ruff!'

// Alternatively you can use the legacy new() function but keep in mind that
// the new operator is faster in most browsers
var dog = Dog.new()
dog.bark()                // 'Ruff! Ruff!'

// Forget about special `instanceof` operator, use JS native method instead:
Dog.prototype.isPrototypeOf(dog)    // true

// Objects inherit from objects, what could be more object oriented than
// that ?
var Pet = Dog.extend({
  initialize: function(breed, name) {
    this.breed = breed
    this.name = name
  },
  call: function(name) {
    return this.name === name ? this.bark() : ''
  },
  toString: function() {
    return this.breed + ' ' + this.name
  }
})

// All arguments passed to the constructor are forwarded to the `initialize`
// method of instance.

var pet = new Pet('Labrador', 'Benzy')
pet.toString()          // 'Labrador Benzy'
pet.call('doggy')       // ''
pet.call('Benzy')       // 'Ruff! Ruff!'

Object composition

// In some programs recombining reusable pieces of code is a better option:

var HEX = Base.extend({
  hex: function hex() {
    return '#' + this.color
  }
})

var RGB = Base.extend({
  red: function red() {
    return parseInt(this.color.substr(0, 2), 16)
  },
  green: function green() {
    return parseInt(this.color.substr(2, 2), 16)
  },
  blue: function blue() {
    return parseInt(this.color.substr(4, 2), 16)
  }
})

var CMYK = Base.extend(RGB.prototype, {
  black: function black() {
    var color = Math.max(Math.max(this.red(), this.green()), this.blue())
    return (1 - color / 255).toFixed(4)
  },
  magenta: function magenta() {
    var K = this.black();
    return (((1 - this.green() / 255).toFixed(4) - K) / (1 - K)).toFixed(4)
  },
  yellow: function yellow() {
    var K = this.black();
    return (((1 - this.blue() / 255).toFixed(4) - K) / (1 - K)).toFixed(4)
  },
  cyan: function cyan() {
    var K = this.black();
    return (((1 - this.red() / 255).toFixed(4) - K) / (1 - K)).toFixed(4)
  }
})

// Composing `Color` prototype out of reusable components:
var Color = Base.extend(HEX.prototype, RGB.prototype, CMYK.prototype, {
  initialize: function initialize(color) {
    this.color = color
  }
})

var pink = Color.new('FFC0CB')
// RGB
pink.red()        // 255
pink.green()      // 192
pink.blue()       // 203

// CMYK
pink.magenta()    // 0.2471
pink.yellow()     // 0.2039
pink.cyan()       // 0.0000

Combining composition & inheritance

var Pixel = Color.extend({
  initialize: function initialize(x, y, color) {
    Color.initialize.call(this, color)
    this.x = x
    this.y = y
  },
  toString: function toString() {
    return this.x + ':' + this.y + '@' + this.hex()
  }
})

var pixel = new Pixel(11, 23, 'CC3399')
pixel.toString()              // 11:23@#CC3399
Pixel.prototype.isPrototypeOf(pixel)    // true

// Pixel instances inhertis from `Color`
Color.prototype.isPrototypeOf(pixel)    // true

// In fact `Pixel.prototype` itself inherits from `Color.prototype`, remember just simple and
// pure prototypal inheritance where objects inherit from objects.
Color.prototype.isPrototypeOf(Pixel.prototype)    // true

TODO

This is a list of things I may introduce in newer versions.

  • Add testling badge
  • Add examples about private variables (closures)
  • Add guidelines on how to use the lib for common cases
  • Add requirejs test
  • Better merge? in-depth merge (like lodash). Two different extend methods ?
  • Do a full prototype chaining ?
var Extra = Base.extend(Foo, Bar, {toto: true});
var instance = new Extra();
Base.prototype.isPrototypeOf(instance); // true :D
Extra.prototype.isPrototypeOf(instance); // true :D
Foo.prototype.isPrototypeOf(instance); // false :(
Bar.prototype.isPrototypeOf(instance); // false :(
  • Clone inner objects:
var someBase = {
  outer: {
    inner: 1
  }
}
var Extra = Base.extend(Foo, Bar, someBase);
var e = new Extra();
e.outer.inner = 3;
someBase.outer.inner === 1; // should be true