So i wrote the following code to add new property and method to the Object prototype, using defineProperty (doing this otherwise would break jQuery):
Object.defineProperty(Object.prototype, "len", {
value: 0,
enumerable : false,
writable: true
});
Object.defineProperty(Object.prototype, "pushById", {
value: function(object)
{
this.len = this.len+1;
this[object['id']] = object;
//alert(JSON.stringify(this)); -Displays collection object with its len value equal to 1
},
enumerable : false
});
var containerObject = {};
var dataObject = {'id':1, 'name':'Jon Snow'};
containerObject.pushById(dataObject);
console.log(containerObject.len); // -Displays 0 when it should be 1
Why do the properties updating not follow outside the method?