Skip to content

Commit

Permalink
Adding getters and setters to binders so non-function properties are …
Browse files Browse the repository at this point in the history
…also accessible.
  • Loading branch information
David Ellis committed Jul 19, 2013
1 parent e91bfb9 commit cef38dc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/binders.js
Expand Up @@ -3,9 +3,16 @@ function binders(obj, funcName) {
return obj[funcName].bind(obj);
} else {
var outObj = {};
for(var key in obj) {
if(obj[key] instanceof Function) outObj[key] = obj[key].bind(obj);
}
Object.keys(obj).forEach(function(key) {
if(obj[key] instanceof Function) {
outObj[key] = obj[key].bind(obj);
} else {
Object.defineProperty(outObj, key, {
get: function() { return obj[key]; },
set: function(val) { obj[key] = val; }
});
}
});
return outObj;
}
}
Expand Down
5 changes: 4 additions & 1 deletion test/test.js
Expand Up @@ -4,7 +4,7 @@ var binders = jscoverage.require(module, '../lib/binders');
var coveralls = require('coveralls');

exports.fullObjBinding = function(test) {
test.expect(4);
test.expect(6);
var testObj = {
foo: "bar",
hello: "world",
Expand All @@ -22,7 +22,10 @@ exports.fullObjBinding = function(test) {
test.equal(Object.keys(boundObj).length, 3, 'only the functions are passed through to the bound object');
boundObj.setFoo('baz');
test.equal(testObj.foo, 'baz', 'the value is properly set on the original object');
boundObj.foo = 'bay';
test.equal(testObj.foo, 'bay', 'the value is properly set on the original object via a setter');
test.equal(boundObj.getHello(), 'world', 'the value is properly taken from the original object');
test.equal(boundObj.hello, 'world', 'the value is property taken from the original object via a getter');
test.equal(boundObj.whatIsThis(), testObj, 'this is equal to the original object');
test.done();
};
Expand Down

0 comments on commit cef38dc

Please sign in to comment.