JavaScript does not currently have this feature. This may happen in the next version, but through proxies . So before that you had to do something rather ugly, for example:
MyObject.prototype.ex = function(fname) { var f = this[fname], args = Array.prototype.slice.call(arguments, 1); if (typeof f === "function") { return f.apply(this, args); } return this.defaultMethod.apply(this, args); };
... and use it as follows:
var obj = new MyObject(); obj.ex("doesntExistInMyObject", "arg", "arg");
( ex
for "execute" since call
too easy to confuse with Function#call
.)
source share