我想将一个类中的一个对象扩展为该类alpha.helper
的一个实例,但Helper
每次alpha.helper
调用函数时都很难设置它,因为我需要传输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");