473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1651
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Duane Lambe | last post by:
Hi folks - site's at http://www.datawire.net/files/intranet.html ; works fine in IE, but open in Firebird/Moz/Opera, and you can see that the word "Home" near the top doesn't move down to line up...
5
by: Norberto Rivera | last post by:
Is there another option that allows me to do what I want to do? Javascript does not allow me (as far as I could see: I don't know much) to delete, rename, or copy a file at the web hosting side....
9
by: Carlis | last post by:
I want to make a program that, when you have the mouse on a word, that word is bold... and when you don't have the mouse on that word, the word is not bold. Excuse me for my poor English :-( But...
4
by: Luke Matuszewski | last post by:
Here are some questions that i am interested about and wanted to here an explanation/discussion: 1. (general) Is the objectness in JavaScript was supported from the very first version of it (in...
7
by: bobd314 | last post by:
Currently, I am having a problem replacing the value of a input box with something else using the innerHTML thing. Right now I have something going <script type="text/javascript"><!-- function...
17
by: Me | last post by:
Hello, I have read all the reason why DOM purists don't like document.write and innerHTML. I would like to be a good programmer and do the right thing and not use them. But I need your help to...
1
by: Alan Callbiter | last post by:
Hey y'all. I'm looking for a javascript scrolly menu that acts in the same way as those navigation menus on HTPC frontends like Windows Media Centre or MediaPortal. Does anyone have a link to...
1
by: piotr.korzeniewski | last post by:
I'm really confused with this one, first please take a look at code below: function Class(obj) { if(!obj) obj = new Object(); obj.Extend = function(obj) { this.__parent__ = new Object();
7
by: Tony Girgenti | last post by:
Hello. I'm trying to undetrstand ASP.NET 2.0 and javascript. When i have a button and i click on it and i see the web broswer progress bar at the bottom do something, does that mean that there...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.