I have a function that takes a string and a list, and returns a function that toggles the string in/out of the list. So if the function gets called twice, the string will not appear in the list, but calling it once, the string will be in the list.
Is there some standard convention (like callback/handler) for a function that generates another function? I'm working with JavaScript.
Thanks.
Code below:
function wrappedToggleGenerator(id, list) {
return function (flag) {
if (flag) {
// Clear the list if flag is true
list.splice(0, list.length);
}
if (list.indexOf(id) == -1) {
list.push(id);
}
else if (list.indexOf(id) != -1) {
list.splice(list.indexOf(id), 1);
}
}
}
EDIT: I have added my code. Thanks for the suggestions, I think I will prefix function generators with "wrapped".
To call a function inside another function, define the inner function inside the outer function and invoke it. When using the function keyword, the function gets hoisted to the top of the scope and can be called from anywhere inside of the outer function.
The name should make it clear what is the purpose of the function. Long names are okay. Make the names as long as needed, but no longer than that. For example, if you have a function createLocalDatabase and there is only one database, replace it with createDatabase.
coding conventions are defined by the project owners, based on some best practices. Its up to you to decide, which conventions you want to follow.
It may be beneficial to have some meaningful name to the method which also includes return value type, but you may as well have a good documentation in place.
In any case, you might want to read on the Hungarian notiation i fyou want to include indicators of intended use to method names. https://en.wikipedia.org/wiki/Hungarian_notation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With