title: Javascript - catch access to property of object
body: <p>Is it possible to capture when a (any) property of an object is accessed, or attempting to be accessed? </p>
  
  <p>Example:</p>
  
  <p>I have created custom object <code>Foo</code></p>
  
  <pre><code>var Foo = (function(){
      var self = {};
      //... set a few properties
      return self;
  })();
  </code></pre>
  
  <p>Then there is some action against <code>Foo</code> - someone tries to access property <code>bar</code></p>
  
  <pre><code>Foo.bar
  </code></pre>
  
  <p>Is there way (prototype, perhaps) to capture this? <code>bar</code> may be undefined on <code>Foo</code>. I could suffice with capturing any attempted access to undefined properties. </p>
  
  <p>For instance, if <code>bar</code> is undefined on <code>Foo</code>, and <code>Foo.bar</code> is attempted, something like:</p>
  
  <pre><code>Foo.prototype.undefined = function(){
      var name = this.name; //name of property they attempted to access (bar)
      Foo[name] = function(){
          //...do something
      };
      return Foo[name];
  }
  </code></pre>
  
  <p>But functional, unlike my example.</p>
  
  <p><strong>Concept</strong> </p>
  
  <pre><code>Foo.* = function(){
  }
  </code></pre>
  
  <p><strong>Background</strong></p>
  
  <p>If I have a custom function, I can listen for every time this function is called (see below). Just wondering if it's possible with property access.</p>
  
  <pre><code>Foo = function(){};
  Foo.prototype.call = function(thisArg){
      console.log(this, thisArg);
      return this;
  }
  </code></pre>