views:

66

answers:

1

Considering the following javascript example:

var myobj = {   func1: function() { alert(name in this) },
                func2: function() { alert(name in this) },
                func3: function() { alert(name in this) }
}

myobj.func2(); // returns true
myobj.func4(); // undefined function

Is it possible to create a 'catch-all' key for myobj that will get called if there is no key/function defined (as in func4()) while retaining the myobj.functionCall() format?

+2  A: 

You're looking for __noSuchMethod__:
http://stackoverflow.com/questions/994143/javascript-getter-for-all-properties

Joel Alejandro
Dude, I never knew Javascript supported this! Win!
Matchu
I just learned about this too :P
Joel Alejandro
Hmmm... I tried it `__noSuchMethod__: function() { alert('caught it') },` but it isn't getting called.
patrick dw
wrong signature: `__noSuchMethod__: function(id, args)`
Joel Alejandro
Interesting... it is getting called when I use Firefox, but not Safari.
patrick dw
Probably because it must be Gecko-restricted... Too bad, Mozilla addressed this method as "Non-standard".
Joel Alejandro
Tried it with the args. No change. Still works in FF, but not Safari.
patrick dw
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/noSuchMethod - at top it says it's "Non-standard". You may have to go with ugly syntax instead.
Matchu
So the answer is that it is not a standard feature of javascript. Sound right?
patrick dw
Unfortunately, no, it's not part of the JavaScript standard.
Joel Alejandro
+1 and accepted. Thanks for the help.
patrick dw