This is an really simple implementation of getters and setters. And inspired by https://github.com/allouis/prototypes/tree/master/getSet.
Just overwrite your function-prototype with the getSet-function
call.
function Test() {}
Test.prototype = getSet();
You can pass in an optional object to use as the key/value store:
function Test() {}
Test.prototype = getSet(Test.prototype);
To add new properties to the object use get('key')
and set('key', 'value') / set({key: 'value'})
:
function Test() {}
Test.prototype = getSet();
var test = new Test();
test.set('foo', 'bar');
test.set({
foz: 'baz'
});
test.get('foo') // => 'bar'
test.get('foz') // => 'baz'
This will not store false or null inside the cache, you need to use !== to be typesafe.