Background: I am working in Knockout and have implemented a custom component loader. Specifically, I am implementing the loadComponent
and createViewModel
methods for dependency injection purposes. So basically, I determine the appropriate constructor by some convention and then call new
on it with the required dependencies.
My question: when I have this..
function model(arg1, arg2) {
this.foo = arg1;
this.bar = arg2;
}
What exactly is the difference between:
var instance = new model(arg1, arg2);
and:
var instance = {};
model.apply(instance, [arg1, arg2]);
Both seem to have the same result. As for my specific use case, I want to do something like:
var instance = {};
model.apply(new Proxy(instance, handler), [arg1, arg2]);
Where handler
would translate something like:
this.foo = 'bar';
into:
this.foo = ko.observable('bar');
..to abstract the clunky observable syntax of Knockout.
- Is this a terrible idea?
- Is there a better / easier way?
- What the implications of doing this instead of
new
?