How/Can I intercept a request to get a method/property on an javascript object and return a custom value instead.
For example,
var obj = {
// This "get" method is where I want to replace the call to get a property of an object.
get : function(propertyName) {
if (propertyName == 'somePropertyThatDoesntExist') { return 1; }
else { return 0; } // Something like this?
};
// Either this method of access,
var myValue1 = obj.somePropertyThatDoesntExist
var myValue2 = obj.someOtherPropertyThatDoesntExist
// Alternatively,
var myValue3 = obj['somePropertyThatDoesntExist']
var myValue4 = obj['someOtherPropertyThatDoesntExist']
So myValue1 and myValue3 will have a value of 1, and myValue2 and myValue4 will have a value of 0.
Currently, myValue1, 2, 3, 4 would all be "undefined".