node package manager

getter

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

getter

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

For node.js:

npm install getter

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

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',
getterfunction () {
return this.foo + this.foo
},
 
setterfunction (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 

For Node.js tests:

grunt test

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

grunt buster