I am looking to enhance one object within a class called alpha.helper
so that it becomes an instance of the Helper
class. The challenge lies in having to set this every time a function within alpha.helper
is called, as I need to transfer the current value of alpha.imperfect
.
var _ = require("underscore");
var Helper = function(imperfect){
var helper = {};
helper.use = function(item){
console.log(item);
console.log(imperfect);
return false;
}
helper.bar = function(item){
console.log(item);
console.log(imperfect);
return false;
}
return helper;
}
var Alpha = function(){
var alpha = {};
alpha.imperfect = {}
alpha.add = function(item){
_.extend(alpha.imperfect, item);
}
alpha.helper = function(){
var helper = new Helper(alpha.imperfect);
return helper;
};
return alpha;
}
var alpha = new Alpha();
alpha.add({"name":"thomas"});
alpha.add({"something":"seven"});
alpha.helper.use("foo");