Yehuda Katz is a member of the Ember.js, Ruby on Rails and jQuery Core Teams; his 9-to-5 home is at the startup he founded, Tilde Inc.. There he works on Skylight, the smart profiler for Rails, and does Ember.js consulting. He is best known for his open source work, which also includes Thor and Handlebars. He travels the world doing open source evangelism and web standards work.

method_missing in JavaScript?

I’ve posted previously about JavaScript 2 (also known as ECMAScript 4). Since I last discussed this topic, the debate over whether to continue with ES4, which extended JavaScript with type annotations and classes, got significantly less cooperative, and apparently came to a head at the meeting of TC99, the committee responsible for the future of JavaScript.

In summary, it seems that the two groups agreed to focus cooperative effort of ECMAScript 3.1, a modest improvement of the current JavaScript that includes some new features like the ability to mark properties as non-enumerable and freezing objects (which can be used to implement classes).

After that is complete, the two groups have agreed to work on a new version of JavaScript dubbed “Harmony”, which will be a more modest evolution of the current JavaScript, minus some of the more ambitious features like namespaces and packages. Other features, like classes, will likely be implemented in terms of new features in ES3.1 like freeze().

Which brings me to the title of my post. Now that it seems as though ES3.1 will be embraced by all the browser vendors moving forward (and will likely be the implemented iteration of JS for at least a little while), I wondered what sorts of features might still make it into ES3.1 before the spec was closed.

Specifically, I’ve long been interested in trying to get method_missing into JavaScript, and in fact, it is already available in Firefox/Spidermonkey as __noSuchMethod__. In fact, Johnson makes use of noSuchMethod in its Ruby proxy.

Half-wishing, half-hoping, I threw the idea of codifying noSuchMethod into the discussion on the ES discussion group. As expected, Brendan Eich replied that it was probably too late to add a feature like this into ES3.1 at this time.

But then, to my surprise, Bill Edney and Scott Shattuck, who were responsible for getting noSuchMethod into Spidermonkey, jumped into the fray urging its inclusion into ES3.1. And Justin Meyer, of JavaScriptMVC, posted to their blog also urging its inclusion.

Anyone who’s messed with Ruby for more than just a simple Rails app understands the power of method_missing, and getting it into ES3.1, which would mean it would make it into future versions of Firefox, Safari, and IE, would be sweet indeed.

15 Responses to “method_missing in JavaScript?”

More than sweet. This is the one thing that prevents JS from being a “true” dynamic language. Add that, and many other requirements just vanish.

Bertrand,

What say you make your position known to the ES3.1 group? Anyone who feels passionately about noSuchMethod and can articulate themselves clearly should definitely head over to the ES group and lobby for its inclusion in the spec.

— Yehuda

Wait, is this like #doesNotUnderstand in Smalltalk?

:)

@michael Yep.

Perhaps something more generic like __keyMissing__ since functions are just objects assigned to an ‘hash’. But either way, I want method_missing. Oooh yeah.

@drnic maybe something like noSuchProperty with sugar on noSuchMethod to produce a function to be returned by noSuchProperty.

Yehuda: I will absolutely do that and talk to our ES3.1 representatives.

Do you know if the JS __noSuchMethod proposal also handles properties that might not exist?

More than sweet. This is the one thing that prevents JS from being a “true” dynamic language.

This would be great, as would __noSuchProperty__…

The lack of it sure does make stubbing objects in JS a PITA unless you want to do it one method at a time….

Er… s/methods/functions/ ;-)

Leave a Reply

Archives

Categories

Meta