How can I specify a default getter for a prototype?
With default getter I mean a function that is called if obj.undefinedProperty123
is called.
I tried Object.prototype.get = function(property) {..} but this is not called in this case.
How can I specify a default getter for a prototype?
With default getter I mean a function that is called if obj.undefinedProperty123
is called.
I tried Object.prototype.get = function(property) {..} but this is not called in this case.
I am not sure about what you are asking. But If you want a method to be called when the user attempts to Access object.nonExistingProperty
. I dont think there is any way to do that.
Firefox it's possible with non-standard noSuchMethod:-
({noSuchMethod:function(){alert(1);}}).a();
What Gareth said, except it's __noSuchMethod__
.
Or maybe you were thinking of PHP?
Here's a very good article on the recently standardized property getters/setters, highlighting some previous non-standard incarnations.
summary: there's no standard 'catch-all' getter / setter (yet), but Object.defineProperty is the future.