The JavaScript blog.


metaprogramming language

__noSuchMethod__ and v8

Posted on .

I saw this today on Thomas Fuchs'
: Allow noSuchMethod
. It's
an issue raised for v8, asking for noSuchMethod support.
The issue references a Bugzilla SpiderMonkey
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();

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:


ruby metaprogramming

Ruby vs. JavaScript Metaprogramming

Posted on .

Ric sent me Metaprogramming: Ruby vs.
and at first I thought it looked pretty basic. However, I've noticed a
lot of DailyJS readers are experienced ruby developers, and there are
some good pointers in this article for rubyists.

For example, if you're used to the metaclass in ruby, you might
over-think metaprogramming in JavaScript:

To be honest, there’s not much to say about the JavaScript example because it is so simple. We avoid the whole metaclass business because JavaScript uses prototypal inheritance. This means that Javascript does not distinguish between classes/prototypes and instances and, therefore, we can add our desired behavior directly to the instance.

On a related note, I've previously done a lot of JavaScript DSL
experimentation and stretched the limits of stylistically acceptable
JavaScript. In Fear and Loathing in JavaScript
I explain various
techniques for giving the illusion of a friendly DSL. However, the best
JavaScript libraries all seem to avoid this and make their interfaces
more explicit, so I've never felt 100% comfortable with the approaches
in that article.