In Ruby, you can capture a method call that is missing and define it on the fly.
What I want to do in JavaScript is to have an object without methods. I want the missing method to be translated into an emit () call:
app.isReady() -> app.emit("isReady") soldier.kills() -> soldier.emit("kills")
I think it is better to fix the missing method error and run emit (methodName), rather than defining all methods (from a fixed list) at runtime. Thus, we do not have performance overhead if there are hundreds or thousands of events for an object.
What is the best way to do this?
UPDATE: this is an API design, so I prefer not to allow:
try { app.isReady() } catch(e) { ... }
I want to know how I can accomplish this behind the scenes so that users can use the methods as usual.
javascript reification sample porting method-missing
ajsie Nov 27 '11 at 3:10 2011-11-27 03:10
source share