469,332 Members | 6,612 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

Is there a thing such as method_missing in Javascript?

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...

Dec 7 '06 #1
4 1591
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".
Dec 7 '06 #2
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".
Dec 7 '06 #3
yeah wrote:
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".
No , I don't think javascript has exactly that feature , but depending
on what your ultimately trying to achieve there may be some hope yet.
Dec 7 '06 #4

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

Dec 7 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Duane Lambe | last post: by
4 posts views Thread by Luke Matuszewski | last post: by
17 posts views Thread by Me | last post: by
1 post views Thread by Alan Callbiter | last post: by
1 post views Thread by piotr.korzeniewski | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.