How do I listen for changes in Object fields that are added dynamically?
<p>I wish to extend the Object and Array classes so that I will be able to listen to any changes made to their instances after construction.</p>

<p>The best I can do now is to add custom get and set functions for fields given at initialization/construction of an Instance:</p>

<pre><code>var myObj = new CustomObject({name:'foo'});

myObj.name = 'bar'; // this will log "'name' in 'myObj' updated:'bar'"

//however:

myObj.age = 85; // this mutation will slip by unnoticed, 
                // since the 'age' field was never specified 
                // at initialization meaning no custom set / get 
                // functions where attached.
</code></pre>

<p><strong>Is there a way to achieve the following functionality?</strong></p>

<pre><code>var myObj = new CustomObject();

myObj.name = 'foo';// this should log something like:
                   // "A new field 'name' was created for myObj with value 'foo'"
</code></pre>

<p><strong>Note:I'm looking for a solution that doesn't involve polling.</strong></p>

<p>What I have now (resulting in the functionality shown in the first codeblock):</p>

<pre><code>function CustomObject(data) {

    var that = this;

    for(var field in data){         
        prepField(field);                                   
    }

    function prepField(field){                  
        Object.defineProperty( that, field, {
            set: function(val){         
                data[field] = val;
                console.log(field,'in',that,'updated:',val);
            },
            get: function(){
                console.log(field,'in',that,'requested');
                return data[field];                 
            },
            enumerable:true
        });         
    }

    return this;

}
</code></pre>

<p>Thanks in advance!</p>
