__noSuchMethod__ and v8
I saw this today on Thomas Fuchs’ twitter: Allow noSuchMethod implementations. It’s an issue raised for v8, asking for __noSuchMethod__ support. The issue references a Bugzilla SpiderMonkey bug that successfully raised the issue and got it implemented, despite it not being in the ECMAScript standards.
If you haven’t seen such a method before, it’s quite simple: a “catchall” method is run whenever a method can’t be found within a given context. This is a major technique for meta-programming in ruby, and is used in other languages as well. This will execute in Rhino:
function ExampleClass() {
}
ExampleClass.prototype.print = function(message) {
print("ExampleClass.prototype.print: " + message)
}
ExampleClass.prototype.__noSuchMethod__ = function() {
this.print("No such method, maybe you should try reading the docs?");
}
example = new ExampleClass();
example.eatPizza();
There are some encouraging comments in the thread:
I would say that both the C-based Spidermonkey JS engine and the Java-based Rhino engine have implemented noSuchMethod and, for us ex-Smalltalkers, having a ‘doesNotUnderstand’-like functionality is very empowering :-).
+1 for this feature. As dachev pointed out above, this will be huge for server-side JS at least for the moment. May be big for the client as well in some time. (FF already does this, AFAIK).
However, the v8 team try to keep compatibility with Safari which doesn’t current support such a method. Given the growing popularity of projects like node.js, I think it’s likely that Safari and v8 will get support.
Further reading: