Last active
October 23, 2018 16:29
-
-
Save sdball/f7dc67713b0952a822bbff513bee83ac to your computer and use it in GitHub Desktop.
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
'use strict'; | |
// none of this is good | |
class Greet { | |
required(arg) { | |
throw new Error(`${arg} is required`); | |
} | |
requireArgs(given, required) { | |
for (let i=0; i<required.length; i++) { | |
if (!given[i]) { | |
throw new Error(`${required[i]} is required`); | |
} | |
} | |
return given.slice(0, required.length); | |
} | |
// easy, but verbose declaration of named arguments | |
hello(name=this.required('name'), color=this.required('color')) { | |
console.log(`hello ${name}, nice ${color} heelys`); | |
} | |
// less easy and clear for no real reason | |
hi(...args) { | |
const [name, color] = this.requireArgs(args, ['name', 'color']); | |
console.log(`hi ${name}, nice ${color} haircut`); | |
} | |
} | |
const greeter = new Greet(); | |
greeter.hello('Chris', 'murple'); | |
// greeter.hello('Chris'); // throws error | |
greeter.hi('Chris', 'greue'); | |
// greeter.hi('Chris'); // throws error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment