Functions should be bound to context or not use this
Yomguithereal opened this issue · 2 comments
Hello @acarl005. The fact that your functions need this
to function has this somewhat uncomfortable side-effect that you cannot use them without calling them from the generatorics object.
Here is what I mean:
// If you do the following for instance:
var combination = require('generatorics').combination;
// This will throw & fail because the function does not have the required scope
combination([1, 2, 3], 2);
A solution would be to bind your function to the correct scope before exporting or rewrite the code marginally not to rely on the scope of the generatorics object.
On a side note, decomposing functions further would also mean someone could import only parts of the library as a result:
var combination = require('generatorics/combination');
I'll be happy to draft a PR if you want.
@Yomguithereal Thanks for the feedback! You're absolutely right. I've always found it annoying when I have to manually re-bind methods to their context. The API could certainly be better. A PR would be greatly appreciated!