Catchall invocation for 'null' and 'undefined' (or: how to be a disruptive influence in 3 easy steps)
William Edney
bedney at technicalpursuit.com
Wed May 6 19:05:12 PDT 2009
All -
Well, Sister Eugenia always did say I was a disruptive influence in
grade school - guess I haven't changed much :-).
Let me throw another idea into the mix and churn the pot a bit more.
Many moons ago when I was a Smalltalker, I always liked the fact that
'nil' in Smalltalk was a singleton instance of UndefinedObject... and
you could add methods to UndefinedObject :-).
Like... doesNotUnderstand.
When I switched to JS, I found out that 'null' and 'undefined' were
'special' and weren't instances of anything I could add methods to
(cue wailing and gnashing of teeth).
I always thought it would be most interesting if the following were
true: 'null' was the singleton instance of the Null type and
'undefined' was the singleton instance of the Undefined type.
Then, I could do the following (which would have saved me a lot of
debugging time over the years):
(Using the older __noSuchMethod__ syntax):
Null.prototype.__noSuchMethod__ = function (id, args)
{
alert('you were a bad boy and tried to send ' + id + ' to null');
}
Undefined.prototype.__noSuchMethod__ = function (id, args)
{
alert('you were a bad boy and tried to send ' + id + ' to undefined');
}
My business partner Scott has bugged Brendan about this in the past
and he mentioned that ES3 was too set in its ways to do anything like
this, but I figured maybe with Harmony on the horizon, the issue could
be revisited :-). I have no idea regarding the implications of such a
change and therefore leave it to you all to consider those implications.
Thoughts?
Cheers,
- Bill
More information about the es-discuss
mailing list