I took a look at the docs for method_missing[1] and it looks a lot like
PHP's overloading "magic methods"[2], which are called when PHP is trying to
access a property/method that isn't defined on the object. If that is so,
then I'm sorry to say that something like this does not exist in JavaScript
to the best of my knowledge (and if it does, it's not cross-browser
compatible). There is an ECMAScript 4 (upon which JavaScript 2 will be
based) proposal for this sort of thing, though[3], but it'll be a while
before we see it cross browser (my prediction: Firefox will get it first --
being owned by the current stewards of JavaScript, followed by Opera and
Safari (not sure of the order though). IE, in its seemingly eternal game of
catch-up, won't get this until we've already moved on to ECMAScript 5, if
that).

[1] http://corelib.rubyonrails.org/classes/Kernel.html#M002044
[2] http://us2.php.net/manual/en/language.oop5.overloading.php. Don't get
confused by their use of the word "overloading", though. They're really more
like "property/method not found" handlers. Overloading like you would find
in C++ (two methods with the same name but different prototypes) does not
exist in PHP.
[3] http://developer.mozilla.org/es4/proposals/catchalls.html

On 4/4/07, Jörn Zaefferer <[EMAIL PROTECTED]> wrote:


chinmay schrieb:
> Hi all,
>
> Usually, I'm this lurker who scrapes this list for tips to improve my
> jquery code. As a beginner, I can't begin to tell you how useful it's
> been.
>
> I was just wondering: Can we have a method_missing or similar
> exception-catching scheme for jQuery? Dunno about you, but I'm pretty
> impressed with Rails dynamic finders (like find_by_name_and_city()
> etc. Perhaps a $(form).find_all_selected() ? )
>
> Maybe we could even use it to reduce the footprint of the core jQuery
> script on a page, pulling extra functionality from the server if we
> find a method_missing.
>
> Can anyone see any use for this? And if someone wants to code this,
> tell me; I'm all game to learn!
>
I don't know how exactly method_missing works in Ruby, but I don't know
of a similar language feature in JavaScript.

Whenever I write code that relies on some optional code, it looks like
this:

if ( someObject.someProperty ) {
    // do something with that property
}

To avoid more then one check of this type in a script you can write a
replacement implementation:

// at the start of your script
if ( !someObject.someProperty ) {
        someObject.someProperty = function() {};
}
...
// now use someProperty and nothing happens when it doesn't exist yet
someObject.someProperty()

--
Jörn Zaefferer

http://bassistance.de




--
Aaron Heimlich
Web Developer
[EMAIL PROTECTED]
http://aheimlich.freepgs.com

Reply via email to