Created
August 12, 2014 01:56
-
-
Save cmawhorter/991fa48d53ad551a6a64 to your computer and use it in GitHub Desktop.
All the joy of loose typing, combined with a little more strictness
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
// Playing with overloads and stricter typing in JS -- https://github.com/cmawhorter/tiptop | |
var fn = require('../lib/tiptop'); | |
function MyObj() { | |
} | |
MyObj.prototype.save = fn.overloaded( | |
function() { | |
return this.save(id); | |
}, | |
function(id$Number) { | |
return this.save(id$Number, name); | |
}, | |
function(id$Number, name$String) { | |
return this.save(id$Number, name$String, dt); | |
}, | |
function(id$Number, name$String, dt$Date) { | |
return DB_Save({ | |
id: id$Number | |
, name: name$String | |
, dt: dt$Date | |
}); | |
} | |
); | |
// or you could also share code. wrapped just because | |
(function() { | |
function sharedCode() { | |
} | |
MyObj.prototype.blah = fn.overloaded( ... overloads that call sharedCode ... ); | |
})() | |
var obj = new MyObj(); | |
obj.save(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment