I want to be able to create a new object that is effectively an alias of an existing object, except with a new method added to the new object that is not added to the original aliased object. I'm new to javascript, so I don't know if something like what I want already exists and/or is easily implimented. To give a simplified example of my use case consider this:
So say I have a shared object foo. For Angular types imagine a service or factory. It looks like this:
var foo(){
var array: {},
updateArray: function(value){ array.push(value)},
saveFoo: function(parameters){
//save logic
}
}
say I get sick of constantly passing huge arrays of parameters to foo's save method, so I decide to add a wrapper to foo with some extra logic to make my life easier
function addSaveableParameters(foo){
var savedParams={}
foo.setParam = function(parameter){
savedParams.push(parameter);
}
foo.save = function(){
foo.save(savedParams);
}
return foo
}
foo=addSaveableParameters(foo);
now If I save a parameter one at a time to foo by simply calling my little helper function to add the saveParameter method.
However, imagine if foo was a shared object, or in angular terms a service/factory. If I call addSavableParameters I'm modifying the one version of foo everyone has. More importantly if I have two people that are using my foo service and they each call saveParameters they will both save a parameter to the same shared foo object, which will likely cause them both to be confused when they call save() and find a second parameter they never added exists on foo.
One option to handle this would be to clone foo. However, the original shared foo had an array that others could modify. Perhaps we want to be aware of changes to the array made by others. By cloning foo I create a new instance which has a mere copy of foo's array as of the time it was cloned, it doesn't stay up to date with new changes to the original shared foo's array.
If I wanted to both stay up to date with changes to Foo's array and have my own separate version of savedParameters I could wrap foo instead:
function addSaveableParameters(foo){
var newFoo={
savedParams: {},
setParam: function(parameter){
savedParams.push(parameter);
},
save: function(){
foo.save(savedParams);
},
save: function(parameters){
foo.save(parameters);
},
updateArray: function(value){
foo.updateArray(value);
}
}
return newFoo;
}
foo=addSaveableParameters(foo);
now I can create my own version of foo, which reflects all changes to the old foo and can generally be used as if it was an alias to the old foo, but which allows me to set saved parameters only on my version of foo.
However, I had to manually wrap functions to do it. If I have a really large object I may find myself wrapping dozens of functions. I also need to know every function foo has ahead of time so I can create a wrapper to it. If someone decides to add a new method to foo later my wrapper won't have the new method.
Is there a convenient way in javascript to make this sort of wrapper logic occur automatically without being written to a specific object? So I can simply call wrapper(foo) and I'll get back an object with all of foos methods and variables, which are still aliased to foo, but where I can add methods which only exist on my version of the wrapped foo object?
Aucun commentaire:
Enregistrer un commentaire