Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pd

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pd

Manage propertyDescriptors, an OO utility

  • 0.6.3
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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)

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..)

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..)

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 list of objects to mix in

var o = {
    constructor: function () { 
        pd.bindAll(this, {
            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

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

Keywords

FAQs

Package last updated on 22 Mar 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc