pd
Manage propertyDescriptors, an OO utility
Last updated 15 years ago by raynos .
Repository · Original npm · Tarball · package.json
$ npm install pd 
SYNC missed versions from official npm registry.

pd Build Status

Helping you do prototypical OO

Status: production ready

Example

var Animal = {
    legs: 4,
    walk: function () { ... }
};

var Cat = pd.extend(Object.create(Animal), {
    nyan: function () { ... },
    constructor: function () { this.lives = 9; return this; }
});

var cat = Object.create(Cat).constructor();

Motivation

ES5 OO is verbose

pd solves this with utilities and sugar.

Blog Posts

Documentation

pd (obj) link

pd converts all the values of your objects properties into property descriptors of those values.

pd({
    "foo": "bar"
})

is the same as

{
    "foo": {
        "value": "bar",
        "enumerable": true,
        "writable": true,
        "configurable": true
    }
}

pd.extend (obj..) link

pd.extend extends an object with other objects. key clashes are given right preference

pd.extend(
    {
        "one": "faz",
        "three": "bar"
    },
    {
        "two": "ni",
        "three": "baz"
    },
    {
        "three": "bas",
        "four": "four"
    }
);

is the same as

{
    "one": "faz",
    "two": "ni",
    "three": "bas",
    "four": "four"
}

pd.extend returns the first object you pass in.

pd.bindAll (obj, optionalWhitelist) link

pd.bindAll is similar to underscore's bindAll method. It takes an object and binds all it's methods to the object. It takes an optional whitelist parameter to only bind some methods

var o = {
    constructor() { 
        pd.bindAll(this, ["draw"]);
    }
    draw: function () { 
        /* use `this` with its "correct" value, i.e. `o` */
    },
    start: function (eventEmitter) {
        // note `this.draw` would not work correctly if it wasn't bound
        eventEmitter.on("draw", this.draw);
    }
};

pd.Name link</sup

pd.Name constructs a Name function. This name function when passed your object will return a privates object. This privates object cannot be accessed in any other way then calling Name.

Example:

var Klass = (function () {
    var privates = pd.Name();

    return {
        constructor: function (secret) {
            privates(this).secret = secret;
        },
        getSecret: function () {
            return privates(this).secret;
        }
    };
}());

Installation

npm install pd

Test

make test

Contributors

  • Raynos
  • Gozala

MIT Licenced

Current Tags

  • 0.8.1                                ...           latest (15 years ago)

28 Versions

  • 0.8.1                                ...           15 years ago
  • 0.8.0                                ...           15 years ago
  • 0.7.4                                ...           15 years ago
  • 0.7.3                                ...           15 years ago
  • 0.7.2                                ...           15 years ago
  • 0.7.1                                ...           15 years ago
  • 0.7.0                                ...           15 years ago
  • 0.6.3                                ...           15 years ago
  • 0.6.2                                ...           15 years ago
  • 0.6.1                                ...           15 years ago
  • 0.6.0                                ...           15 years ago
  • 0.5.0                                ...           15 years ago
  • 0.4.2                                ...           15 years ago
  • 0.4.1                                ...           15 years ago
  • 0.4.0                                ...           15 years ago
  • 0.3.9                                ...           15 years ago
  • 0.3.8                                ...           15 years ago
  • 0.3.7                                ...           15 years ago
  • 0.3.6                                ...           15 years ago
  • 0.3.5                                ...           15 years ago
  • 0.3.4                                ...           15 years ago
  • 0.3.3                                ...           15 years ago
  • 0.3.1                                ...           15 years ago
  • 0.3.0                                ...           15 years ago
  • 0.2.1                                ...           15 years ago
  • 0.1.1                                ...           15 years ago
  • 0.1.0                                ...           15 years ago
  • 0.0.1                                ...           15 years ago
Maintainers (1)
Downloads
Total 109
Today 0
This Week 2
This Month 56
Last Day 2
Last Week 14
Last Month 9
Dependencies (0)
None
Dev Dependencies (1)
Dependents (3)

© 2010 - cnpmjs.org x YWFE | Home | YWFE