Skip to content

Instantly share code, notes, and snippets.

@Kattoor
Created October 11, 2016 13:50
Show Gist options
  • Save Kattoor/657d13e927c1807d0fcbbbe6df807e83 to your computer and use it in GitHub Desktop.
Save Kattoor/657d13e927c1807d0fcbbbe6df807e83 to your computer and use it in GitHub Desktop.
"use strict";
function overload(f1, f2) {
if (f1.length === f2.length)
throw new TypeError;
else
return function(...args) {
let params = Array.prototype.splice.call(arguments, 0);
if (params.length === f1.length)
return f1(...params);
else if (params.length === f2.length)
return f2(...params);
else
throw new TypeError;
}
}
var createVector = overload(
(a, b) => { return { x: a, y: b } },
(length) => { return { x: length / 1.414, y: length / 1.414 } }
);
console.log(createVector(3, 4));
console.log(createVector(7.07));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment