Skip to content

Instantly share code, notes, and snippets.

@timruffles
Created July 12, 2011 23:28
Show Gist options
  • Save timruffles/1079391 to your computer and use it in GitHub Desktop.
Save timruffles/1079391 to your computer and use it in GitHub Desktop.
inadvisably late-night js message passing
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