Created
November 10, 2015 17:31
-
-
Save jsteinbeck/6b21b2b8ecadbc0d0db2 to your computer and use it in GitHub Desktop.
Custom multimethod dispatch using the typeof operator (ENJOY.js)
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 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