Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Is there a thing such as method_missing in Javascript?

Reply
Thread Tools

Is there a thing such as method_missing in Javascript?

 
 
yeah
Guest
Posts: n/a
 
      12-07-2006
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...

 
Reply With Quote
 
 
 
 
yeah
Guest
Posts: n/a
 
      12-07-2006
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".


 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      12-07-2006

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is there such a thing as xsl code asist in wtp 1? Elhanan Java 1 04-04-2006 03:55 AM
RegEx: Is there such a thing as "non-greedy backwards"? mrclean_ii@hotmail.com Java 5 01-25-2005 04:07 PM
Is there such a thing? BasketCase Computer Support 7 12-09-2003 01:54 PM
The term Browser ID?? no such thing is there? moo moo Java 2 11-20-2003 12:25 PM
Is there such a thing as a JVM monitor... Edward A Thompson Java 3 10-02-2003 10:32 PM



Advertisments