Skip to content

Instantly share code, notes, and snippets.

@sdball
Last active Oct 23, 2018
Embed
What would you like to do?
'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