Code Monkey home page Code Monkey logo

describe-property's Introduction

npm package build status dependency status code climate

describe-property is a property descriptor library that runs in both node.js and the browser. You use it to quickly generate property descriptors to use with Object.create, Object.defineProperty, and/or Object.defineProperties.

Example

var d = require('describe-property');

function Person(firstName, surname) {
  this.firstName = firstName;
  this.surname = surname;
}

Object.defineProperties(Person.prototype, {

  // Methods can be passed directly.
  sayHi: d(function () {
    console.log('Hello, my name is', this.fullName);
  }),

  // Getters are defined using d.gs.
  fullName: d.gs(function () {
    return this.firstName + ' ' + this.surname;
  }),

  // Setters are defined as the second argument to d.gs.
  firstName: d.gs(function () {
    return this._firstName;
  }, function (value) {
    this._firstName = value.trim();
  })

});

By default property descriptors use ES5 attributes.

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

But any of these can be overridden using an object literal.

d({
  enumerable: true,
  value: function () {
    // ...
  }
}); // => { configurable: true, enumerable: true, writable: true, value: function () {} }

Installation

Using npm:

$ npm install describe-property

Issues

Please file issues on the issue tracker on GitHub.

Tests

To run the tests in node:

$ npm install
$ npm test

Credits

This library was inspired by @medikoo's excellent d library. It is intended to be a lighter-weight alternative with fewer features, but also only a single dependency.

License

MIT

describe-property's People

Contributors

mjackson avatar

Stargazers

 avatar  avatar  avatar

Watchers

 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.