views:

148

answers:

3

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.

+1  A: 

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.

That's just what I want to do.I think it is possible and I think I remember that I did this before - if I'd just know how...
Manuel
A: 

Firefox it's possible with non-standard noSuchMethod:-

({noSuchMethod:function(){alert(1);}}).a();

Gareth Heyes
+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.

http://whereswalden.com/2010/04/16/more-spidermonkey-changes-ancient-esoteric-very-rarely-used-syntax-for-creating-getters-and-setters-is-being-removed/

summary: there's no standard 'catch-all' getter / setter (yet), but Object.defineProperty is the future.

no