getter

v0.0.3
Helper for adding getters and setters into your object. For node and browser.
getter setter

getter Build Status

Helper for adding getters and setters into your object. For node and browser.

Installation

For node.js:

npm install getter

For browser you need to include lib/getter.js into your page. Getter is AMD-friendly.

Usage

All you need to do is to pass an object and options to mixin function. Getter will create getters and setter for you:

var getter = require('getter');
var obj = {};

getter.mixin(obj, {
    foo: 'bar',
    baz: 1
});

console.log(obj.getFoo()); // "bar"

obj.setBaz(2);
console.log(obj.getBaz()); // 2

You can easily override default getters and setters:

var getter = require('getter');
var obj = {};

getter.mixin(obj, {
    foo: {
        value: 'bar',
    
        getter: function () {
            return this.foo + this.foo
        },

        setter: function (val) {
            this.foo = val + val;
        }
    }
});

console.log(obj.getFoo()); // "barbar"

obj.setFoo('x');
console.log(obj.getFoo()); // "xxxx"

If you don't want to create getter of setter for property, you can always get rid of it:

var getter = require(getter);
var obj = {};

getter.mixin(obj, {
    foo: {
        value: 'bar',
        setter: false
    }
});

console.log('setFoo' in obj); // false

Tests

For Node.js tests:

grunt test

For all tests (buster and phantomjs must be installed globally):

grunt buster
npm i getter

Metadata

  • Unknown
  • >=0.8.0
  • PaweÅ‚ Maciejewski
  • released 11/5/2012

Downloads

Maintainers