title: How to autowrap a shared object to alias existing functionality while adding instant specific private values
body: <p>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:</p>
  
  <p>So say I have a shared object foo.  For Angular types imagine a service or factory.  It looks like this:</p>
  
  <pre><code>var foo(){
      var array: {},
      updateArray: function(value){ array.push(value)},
      saveFoo: function(parameters){
           //save logic
      }
  }
  </code></pre>
  
  <p>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</p>
  
  <pre><code>function addSaveableParameters(foo){
        var savedParams={}
        foo.setParam = function(parameter){
            savedParams.push(parameter);
        }
        foo.save = function(){
            foo.save(savedParams);
        }
  
        return foo
  }
  
  foo=addSaveableParameters(foo);
  </code></pre>
  
  <p>now If I save a parameter one at a time to foo by simply calling my little helper function to add the saveParameter method.</p>
  
  <p>However, I'm working with a shared object, an angular factory specifically.  If I call addSavableParameters I'm modifying the one version of the foo factory 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 later call save() and find a second parameter they never added exists on foo.</p>
  
  <p>One option to handle this would be to clone foo.  However, the original shared foo had an array that others could modify.  I <em>want</em> to be aware of changes to that array, if someone changes the original foo's array I want to see those changes, and if I change foo I want others to see my changes.  By cloning foo I would 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.</p>
  
  <p>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:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>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 <em>my</em> version of foo.</p>
  
  <p>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.</p>
  
  <p>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 <em>my</em> version of the wrapped foo object?</p>