Does Javascript have something like Ruby's method_missing feature?
The ruby feature that you are explaining is called "method_missing" http://rubylearning.com/satishtalim/ruby_method_missing.htm.
It's a brand new feature that is present only in some browsers like Firefox (in the spider monkey Javascript engine). In SpiderMonkey it's called "__noSuchMethod__" https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/NoSuchMethod
Please read this article from Yehuda Katz http://yehudakatz.com/2008/08/18/method_missing-in-javascript/ for more details about the upcoming implementation.
Method missing in JS
Short answer
Does JavaScript have an equivalent to
__getattr__
?
No :(
Long answer
It looks like you just want to map aliases, JavaScript has no problem with adding properties to things like this
// set up
function Converter(amount) {
this.amount = amount;
}
Converter.prototype.rupy = function () {
return this.amount * 2;
};
// add aliases
var original = 'rupy', aliases = ['rupies'], i;
for (i = 0; i < aliases.length; ++i)
Converter.prototype[aliases[i]] = Converter.prototype[original];
Now
var foo = new Converter(1);
foo.rupies(); // 2
and have
foo.rupies === foo.rupy; // true
Future answer
In ECMAScript 6 (Harmony) we have Proxies
Constructor.prototype = Proxy(Constructor.prototype, {
'get': function (target, name, child) {
var o = child || target; /* =proxy? if proxy not inherited? */
if (name in target) return o[name];
if (name === 'rupies') return o['rupy'];
return;
},
'set': function (target, name, val, child) {
return target[name] = val;
},
'has': function (target, name) {
return name in target; // do you want to make 'rupies' visible?
},
'enumerate': function (target) {
for (var key in target) yield key;
}
});
Are there equivalents to Ruby's method_missing in other languages?
Some use cases of method_missing
can be implemented in Python using __getattr__
e.g.
class Roman(object):
def roman_to_int(self, roman):
# implementation here
def __getattr__(self, name):
return self.roman_to_int(name)
Then you can do:
>>> r = Roman()
>>> r.iv
4
Equivalent of Ruby method_missing in Objective C / iOS
Objective-C is dynamic, although having been a Ruby programmer, I would say it is not quite as dynamic as Ruby.
Objective-C does have an equivalent of method_missing. You'll want to override both forwardInvocation:
and methodSignatureForSelector:
and follow this important advice from Apple:
Important To respond to methods that your object does not itself recognize, you must override methodSignatureForSelector: in addition to forwardInvocation:. The mechanism for forwarding messages uses information obtained from methodSignatureForSelector: to create the NSInvocation object to be forwarded. Your overriding method must provide an appropriate method signature for the given selector, either by preformulating one or by asking another object for one.
Don't use doesNotRecognizeSelector:
as Apple warns that it must always result in an exception being thrown.
Please see the NSObject class documentation for further details: http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html
Capture method missing in Javascript and do some logic?
In that way we don't have a performance overhead if there are hundreds/thousands of events for an object.
I think it's a massive misconception to think the performance overhead of adding methods to an object is smaller then the performance overhead of converting method invocations into emit calls.
However you cannot implement this feature in ES5
One could however implement this using Harmony proxies.
I recommend looking at simulating __noSuchMethod__
.
I believe ES6 proxies are experimental and can be turned on in V8 so you could use them with node.js today.
doesNotUnderstand for JavaScript?
There is no catch-all method defined in the JavaScript standard, but Mozilla implements the non-standard __noSuchMethod__
for SpiderMonkey and Rhino (including Firefox obviously).
You may also be interested in checking out @CMS' answer given to the following Stack Overflow question:
- Is there an equivalent of the noSuchMethod feature for properties, or a way to implement it in JS?
Related Topics
How to Scroll to Top of Page With JavaScript/Jquery
Pure JavaScript Method to Wrap Content in a Div
Why Is It Bad Practice to Use Links With the JavaScript: "Protocol"
Replace HTML Page With Contents Retrieved Via Ajax
Changing Nav-Bar Color After Scrolling
How to Show Alternate Image If Source Image Is Not Found? (Onerror Working in Ie But Not in Mozilla)
Saving Binary Data as File Using JavaScript from a Browser
How to Embed an Autoplaying Youtube Video in an Iframe
How to Detect a Mobile Device With JavaScript
Input Value Is a String Instead of a Number
How to Fix Blurry Text in My Html5 Canvas
Sound Effects in JavaScript/Html5
Run JavaScript Function When User Finishes Typing Instead of on Key Up