Skip to content

Instantly share code, notes, and snippets.

@jsteinbeck
Created November 10, 2015 17:31
Show Gist options
  • Save jsteinbeck/6b21b2b8ecadbc0d0db2 to your computer and use it in GitHub Desktop.
Save jsteinbeck/6b21b2b8ecadbc0d0db2 to your computer and use it in GitHub Desktop.
Custom multimethod dispatch using the typeof operator (ENJOY.js)
var method = require("enjoy-js").method;
var dispatch = require("enjoy-js").dispatch;
var specialize = require("enjoy-js").specialize;
var concat = method();
dispatch(concat, type_of, type_of);
specialize(concat, "string", "string", function (a, b) {
return a + b;
});
specialize(concat, "number", "number", function (a, b) {
return ("" + a) + ("" + b);
});
function type_of (thing) {
return typeof thing;
}
// Using it:
console.log(concat("foo", "bar"));
console.log(concat(1.2, 34));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment