easytip: an object as a function of its messages.
I just found this one, not really a stunning pattern, but it could be useful for someone that needs to use just one function name to multiple purposes:
var adam = (function() {
var firstname = "Adam";
var lastname = "of Eden";
return function(message) {
switch (message) {
case "getFirstName":
return firstname;
case "getLastName":
return lastname;
case "getFullName":
return firstname + " " + lastname;
default:
throw "unknown message: " + message;
}
};
}());
adam("getFirstName"); // "Adam"
adam("getLastName"); // "of Eden"
adam("getFullName"); // "Adam of Eden"
adam("getTestName"); // "unknown message: getTestName"
credits to the great @petermichaux:
Written by Francesco Zaia
Related protips
2 Responses
Mhmm, seems your example will always return the full name.
over 1 year ago
·
oops fixed! Thanks!
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Related Tags
#javascript
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#