Skip to content

Instantly share code, notes, and snippets.

@sdball
Last active October 23, 2018 16:29
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 sdball/f7dc67713b0952a822bbff513bee83ac to your computer and use it in GitHub Desktop.
Save sdball/f7dc67713b0952a822bbff513bee83ac to your computer and use it in GitHub Desktop.
'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