There's a relatively common problem confusing users of ES6. This code demonstrates the issue.
const repeater = {
name: 'Repeater',
types: [
{ f: 'function' },
{ n: 'number' }
],
repeat (f, n) {
if (typeof f === 'function') {
f();
} else {
throw new Error('repeat: A Function is required.');
}
n -= 1;
if (!n) {
return undefined;
}
return repeat(f, n);
}
};
repeater.repeat(() => {console.log('foo');}, 10);
foo
ReferenceError: repeat is not defined
The problem is that unlike all typical function expressions, methods don't have lexical bindings that correspond to the function name identifier. In order for this to work, you have to use the form:
repeat: function repeat (f, n) { //...
There's a custom ESLint rule to catch this and help you fix it.