title: javascript how to deep bind an object to a new 'this' value
body: <p>I have a value which may be a primitive or a function or an object containing primitives/functions/objects recursively.</p>
  
  <p>Given a <code>theThis</code> argument, how can I deep bind all functions which may be inside my value to <code>theThis</code> ?</p>
  
  <p>I tried something like:</p>
  
  <pre><code>function deepBind(o, ths) { 
    Object.getOwnPropertyNames(o).forEach(key =&gt; { 
      const desc=Object.getOwnPropertyDescriptor(o, key);
      if (typeof desc === "function") Object.defineProperty(o, key, key.bind(ths)); 
      if (Object.getOwnPropertyNames(key).length&gt;0) deepBind(o.key, ths);
    });
  }
  </code></pre>
  
  <p>but that failed :(</p>
  
  <p>I looked at some solutions like <a href="https://github.com/jonschlinkert/deep-bind/blob/master/index.js" rel="nofollow noreferrer">https://github.com/jonschlinkert/deep-bind/blob/master/index.js</a> but that is not standalone.</p>
  
  <p>I am looking for a <code>deepBind(val, theThis)</code> solution which is standalone.
  I need the solution to also cover getters and setters.</p>
  
  <p>Thx!</p>