Skip to content

Instantly share code, notes, and snippets.

@alunny
Created December 10, 2009 18:15
Show Gist options
  • Save alunny/253533 to your computer and use it in GitHub Desktop.
Save alunny/253533 to your computer and use it in GitHub Desktop.
noSuchMethod / methodMissing -ish interface in vanilla javascript
var Foo = function Foo() {
var someValue = 1;
this.noSuchMethodHandler(id, args) {
if (id == "increment") {
return someValue++;
}
}
};
Foo.prototype.exec = function(id,args) {
if (this[id] && (typeof this[id]=="function")) {
return this[id](args);
} else {
// noSuchMethod
return this.noSuchMethodHandler(id,args);
}
}
var k = new Foo();
k.exec("increment"); // 1
k.exec("increment"); // 2
k.exec("increment"); // 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment