Created
October 23, 2018 14:05
-
-
Save leolanese/7394c698417782eaa210fd6537cb045e to your computer and use it in GitHub Desktop.
Function overloading OOP
This file contains hidden or 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
Function overloading OOP | |
// overload declaration | |
function sum(a: number, b: number): number; | |
function sum(a: string, b: number): number; | |
function sum(a: number, b: string): number; | |
function sum(a: string, b: string): number; | |
// overload definiton | |
function sum(a,b){ | |
if(typeof a === "string"){ | |
a = parseInt(a,10); | |
} | |
if(typeof b === "string"){ | |
b = parseInt(b,10); | |
} | |
return a + b; // add to string (after parseInt them) | |
} | |
sum(1,2); | |
sum("1","2"); // send 2 strings |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment