Giter Site home page Giter Site logo

d's Introduction

D

Property descriptor factory

Originally derived from es5-ext package.

Defining properties with descriptors is very verbose:

var Account = function () {};
Object.defineProperties(Account.prototype, {
  deposit: { value: function () {
      /* ... */
    }, configurable: true, enumerable: false, writable: true },
  whithdraw: { value: function () {
      /* ... */
    }, configurable: true, enumerable: false, writable: true },
  balance: { get: function () {
      /* ... */
    }, configurable: true, enumerable: false }
});

D cuts that to:

var d = require('d');

var Account = function () {};
Object.defineProperties(Account.prototype, {
  deposit: d(function () {
    /* ... */
  }),
  whithdraw: d(function () {
    /* ... */
  }),
  balance: d.gs(function () {
    /* ... */
  })
});

By default, created descriptor follow characteristics of native ES5 properties, and defines values as:

{ configurable: true, enumerable: false, writable: true }

You can overwrite it by preceding value argument with instruction:

d('c', value); // { configurable: true, enumerable: false, writable: false }
d('ce', value); // { configurable: true, enumerable: true, writable: false }
d('e', value); // { configurable: false, enumerable: true, writable: false }

// Same way for get/set:
d.gs('e', value); // { configurable: false, enumerable: true }

Installation

$ npm install d

To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack

Other utilities

autoBind(obj, props) (d/auto-bind)

Define methods which will be automatically bound to its instances

var d = require('d');
var autoBind = require('d/auto-bind');

var Foo = function () { this._count = 0; };
autoBind(Foo.prototype, {
  increment: d(function () { ++this._count; });
});

var foo = new Foo();

// Increment foo counter on each domEl click
domEl.addEventListener('click', foo.increment, false);

lazy(obj, props) (d/lazy)

Define lazy properties, which will be resolved on first access

var d = require('d');
var lazy = require('d/lazy');

var Foo = function () {};
lazy(Foo.prototype, {
  items: d(function () { return []; })
});

var foo = new Foo();
foo.items.push(1, 2); // foo.items array created

Tests Build Status

$ npm test

d's People

Contributors

bowd avatar medikoo avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.