Example code:
var someVar = {};
someVar.text = "some text";
var thisFunc = function(){
this.subfunc = function(){
}
}
How can I assign thisFunc to someVar? If I do someVar = new thisFunc(), someVar.text will be gone.
Thank you.
It seems that an extend function would come in handy in your situation. It's a little helper function, that assigns properties of one object to another one (if you are using a third party library like Underscore, you might already have such a function available):
function extend(obj1, obj2) {
for (var key in obj2) {
if (Object.prototype.hasOwnProperty.call(obj2, key)) {
obj1[key] = obj2[key];
}
}
return obj1;
}
Then you can do:
var someVar = {};
someVar.text = "some text";
var thisFunc = function(){
this.subfunc = function(){
}
}
someVar = extend(thisFunc, someVar);
someVar.textafternew thisFunc?