"missing_method" in javascript

Discussion in 'Javascript' started by pedz, Jul 28, 2011.

  1. pedz

    pedz Guest

    Google tells me to "Please try again"... I apologize if this is a duplicatemessage.

    Is there a way to implement the equivalent of the "missing_method" method in Ruby? If you call foo.whatever(args) in Ruby and the whatever method is not defined, then it will call the missing_method method if it is defined passing it "whatever" along with the args.

    I realize that I can implement this with defining a function "blah" and then just do foo.blah("whatever", args) but the "blah" is distracting. It is much clearer to call foo.whatever(args). Is there a way to do that in Javascript?

    Note, that usually the "whatever" is not a single or even a few possible names but is usually a pattern or a large set such that defining all of them is either not possible until run time or impractical.

    Thank you,
    pedz
    pedz, Jul 28, 2011
    #1
    1. Advertising

  2. pedz <> writes:

    > Is there a way to implement the equivalent of the "missing_method" method in Ruby? If you call foo.whatever(args) in Ruby and the whatever method is not defined, then it will call the missing_method method if it is defined passing it "whatever" along with the args.


    Not yet.
    ES6 migth contain proxies which can be used to do this, but the
    current version of ECMAScript can't do it.

    /L
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
    Lasse Reichstein Nielsen, Jul 28, 2011
    #2
    1. Advertising

  3. On Wed, 27 Jul 2011 at 18:30:57, in comp.lang.javascript, pedz wrote:
    >Google tells me to "Please try again"... I apologize if this is a
    >duplicate message.
    >
    >Is there a way to implement the equivalent of the "missing_method"
    >method in Ruby? If you call foo.whatever(args) in Ruby and the
    >whatever method is not defined, then it will call the missing_method
    >method if it is defined passing it "whatever" along with the args.
    >
    >I realize that I can implement this with defining a function "blah" and
    >then just do foo.blah("whatever", args) but the "blah" is distracting.
    >It is much clearer to call foo.whatever(args). Is there a way to do
    >that in Javascript?
    >
    >Note, that usually the "whatever" is not a single or even a few
    >possible names but is usually a pattern or a large set such that
    >defining all of them is either not possible until run time or
    >impractical.


    It sounds as though you're trying to use properties for something they
    aren't good for. Have you considered other ways? E.g holding names in a
    sorted array.

    John
    --
    John Harris
    John G Harris, Jul 28, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mcdeveloper
    Replies:
    1
    Views:
    4,172
    mcdeveloper
    Jun 13, 2006
  2. CRON
    Replies:
    24
    Views:
    200,456
    Adrienne Boswell
    Jun 20, 2006
  3. Mark Rae

    JavaScript or not JavaScript

    Mark Rae, Sep 5, 2006, in forum: ASP .Net
    Replies:
    36
    Views:
    1,067
    Paul Sture
    Sep 9, 2006
  4. Nathan Sokalski
    Replies:
    4
    Views:
    568
    PJ on Development
    Nov 8, 2007
  5. Lisa Calla
    Replies:
    0
    Views:
    261
    Lisa Calla
    Nov 9, 2004
Loading...

Share This Page