Catch access to undefined property in JavaScript
Posted
by avri
on Stack Overflow
See other posts from Stack Overflow
or by avri
Published on 2010-05-03T06:26:07Z
Indexed on
2010/05/04
6:18 UTC
Read the original article
Hit count: 236
The Spider-Monkey JavaScript engine implements the noSuchMethod callback function for JavaScript Objects.
This function is called whenever JavaScript tries to execute an undefined method of an Object.
I would like to set a callback function to an Object that will be called whenever an undefined property in the Object is accessed or assigned to.
I haven't found a noSuchProperty function implemented for JavaScript Objects and I am curios if there is any workaround that will achieve the same result.
Consider the following code:
var a = {};
a.__defineGetter__("bla", function(){alert(1);return 2;});
alert(a.bla);
It is equivalent to [alert(1);alert(2)]
- even though a.bla is undefined.
I would like to achieve the same result but to unknown properties (i.e. without knowing in advance that a."bla" will be the property accessed)
© Stack Overflow or respective owner