Is there a thing such as method_missing in Javascript?

Discussion in 'Javascript' started by yeah, Dec 7, 2006.

  1. yeah

    yeah Guest

    In Ruby I can define a special method_missing method in my class like
    so:

    class MyClass

    def do_something
    "did it"
    end

    def method_missing
    "could not do that, let's do something instead: " + do_something
    end

    end

    Consequently the following statements would have the indicated results:

    obj = new MyClass
    obj.do_something #--> "did it"
    obj.do_something_else #--> "could not do that, let's do something
    instead: did it"

    Is such a thing possible in JavaScript? Note: I do not know at design
    time which method could possible be called on my objects...
    yeah, Dec 7, 2006
    #1
    1. Advertising

  2. yeah

    yeah Guest

    I think you did not get my question. Whenever I call a method on obj
    that hasn't been defined in that class, method_missing is executed.
    Acts like a fallback...


    drclue wrote:
    > yeah wrote:
    > > In Ruby I can define a special method_missing method in my class like
    > > so:
    > >
    > > class MyClass
    > >
    > > def do_something
    > > "did it"
    > > end
    > >
    > > def method_missing
    > > "could not do that, let's do something instead: " + do_something
    > > end
    > >
    > > end
    > >
    > > Consequently the following statements would have the indicated results:
    > >
    > > obj = new MyClass
    > > obj.do_something #--> "did it"
    > > obj.do_something_else #--> "could not do that, let's do something
    > > instead: did it"
    > >
    > > Is such a thing possible in JavaScript? Note: I do not know at design
    > > time which method could possible be called on my objects...
    > >

    >
    >
    > It sounds like your talking about javascript's "prototype".
    yeah, Dec 7, 2006
    #2
    1. Advertising

  3. yeah

    RobG Guest

    yeah wrote:
    > In Ruby I can define a special method_missing method in my class like
    > so:
    >
    > class MyClass
    >
    > def do_something
    > "did it"
    > end
    >
    > def method_missing
    > "could not do that, let's do something instead: " + do_something
    > end
    >
    > end
    >
    > Consequently the following statements would have the indicated results:
    >
    > obj = new MyClass
    > obj.do_something #--> "did it"
    > obj.do_something_else #--> "could not do that, let's do something
    > instead: did it"
    >
    > Is such a thing possible in JavaScript? Note: I do not know at design
    > time which method could possible be called on my objects...


    You can check if a method exists using:

    if (typeof obj.do_something == 'function'){ /* use do_something */ }

    However if you don't know that the method exists, how do you know how
    to use it?

    Some libraries have a "tryThese" function that use try..catch to try
    functions in succession until one works, which is an abomination to
    anyone with any interest in a quality design.

    I think you are better off to say what you are trying to do at an
    architectural level so that you get suggestions on how to best achieve
    that using javascript, rather than asking how to implement a solution
    from another language.

    For example, instead of using try..catch, you can write your functions
    to check internally if they have "worked" and return false if they
    haven't. Then you can try them in sequence until one returns !false.
    Or design a test up-front to work out which one to call, or to change
    the function referenced by a particular variable.

    There are many ways to skin a cat - your criteria for success and
    available tools will determine which is best.

    --
    Rob
    RobG, Dec 7, 2006
    #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. Edward A Thompson

    Is there such a thing as a JVM monitor...

    Edward A Thompson, Oct 2, 2003, in forum: Java
    Replies:
    3
    Views:
    4,652
    Michael Borgwardt
    Oct 2, 2003
  2. moo moo
    Replies:
    2
    Views:
    375
    Henrique Seganfredo
    Nov 20, 2003
  3. Replies:
    5
    Views:
    836
  4. Elhanan
    Replies:
    1
    Views:
    354
    IchBin
    Apr 4, 2006
  5. Charles Law
    Replies:
    26
    Views:
    841
    Jon Paal
    May 4, 2006
Loading...

Share This Page