Created
December 10, 2009 18:15
-
-
Save alunny/253533 to your computer and use it in GitHub Desktop.
noSuchMethod / methodMissing -ish interface in vanilla javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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