"missing_method" in javascript

P

pedz

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
 
L

Lasse Reichstein Nielsen

pedz said:
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
 
J

John G Harris

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top