Created
July 12, 2011 23:28
-
-
Save timruffles/1079391 to your computer and use it in GitHub Desktop.
inadvisably late-night js message passing
This file contains hidden or 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
| function NoMethodError(method) { | |
| this.name = "NoMethodError"; | |
| this.message = method + " is not defined"; | |
| } | |
| NoMethodError.prototype = Error.prototype; | |
| Object.prototype.methodMissing = function(method,args) { | |
| throw new NoMethodError(method) | |
| } | |
| Object.prototype.send = function(message) { | |
| var args = [].slice.call(arguments,1); | |
| if(this[message]) | |
| this[message].apply(this,args) | |
| else | |
| this.methodMissing(message,args) | |
| } | |
| var userList = { | |
| _name: "Online users", | |
| _displayedUser: {}, | |
| "displayedUser=": function(who) { | |
| if(this.isValid(who)) this._displayedUser = who; | |
| }, | |
| isValid: function(user) { | |
| return user.age > 18; | |
| }, | |
| "<=>": function(list) { | |
| var diff = this.length - list.length; | |
| return diff === 0 ? 0 : diff < 0 ? -1 : 1; | |
| } | |
| }, | |
| tim = { | |
| age: 23 | |
| }; | |
| // src: userList.displayedUser = tim | |
| userList.send("displayedUser=",tim); | |
| userList.send("name=","Who is online now"); | |
| // src: userList > otherList | |
| // userList.send(">",otherlist); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment