Skip to content

Instantly share code, notes, and snippets.

@travishen
Last active June 26, 2018 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save travishen/56dac1acbdebeed9cd0c19124e7b56a9 to your computer and use it in GitHub Desktop.
Save travishen/56dac1acbdebeed9cd0c19124e7b56a9 to your computer and use it in GitHub Desktop.
in JavaScript, functions are objects, so there is actually no overloading, but we can do encapsulations like this
function greet(firstname, lastname, language) {
language = language || 'en';
if (language === 'en') {
console.log('Hello ' + firstname + ' ' + lastname);
}
if (language === 'es') {
console.log('Hola ' + firstname + ' ' + lastname);
}
}
function greetEnglish(firstname, lastname) {
greet(firstname, lastname, 'en');
}
function greetSpanish(firstname, lastname) {
greet(firstname, lastname, 'es');
}
greetEnglish('John', 'Doe');
greetSpanish('John', 'Doe');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment