For the few of us here that use JavaScript, I’d like to tell you about a neat little thing you can do that’s INSANELY useful: the arrow function (aka lambda function)!
An arrow function acts exactly as a function; you can give it parameters and you can run whatever code you want with those parameters, but it ALSO acts like a variable! It’s basically a one-time-use function that you can define as a variable, and every time that variable is called, that code runs. You can create one by typing () => {}. An example of how to make one is:
let numberOperation = (a: number, b: number) => {return a + b}
Now, if you were to call game.splash(numberOperation(1, 2), it would show 3! What’s great about an arrow function is that you can change it completely at any time. If you no longer wanted it to add the numbers, and wanted it to subtract it instead, you can simply do:
numberOperation = (a: number, b: number) => {return a - b}
and it’ll completely change to that. This will let you easily make modular features that change frequently.
That’s still not the best part of this feature, the best part is that you can put it into an array! Yes, an array can be made out of arrow functions, and if you call the array at a specific index, the function at that index will run! This is incredibly useful for things like a custom main menu, move sets, varying actions, and more! To make one, you simply do what I did before, but in an array like so:
let listOfFunctions = [
(a, b) => {return a + b},
(a, b) => {return a - b},
(a, b) => {return a * b},
(a, b) => {return a / b}
]
Now, if you call listOfFunctions[2], a and b will be multiplied, as that’s the function that’s at that index. You can do a lot more with this than just numbers, obviously. Everything from sprites, tilemaps, and custom classes can be put into an arrow function, and any parameter that works in a regular function works in this, too! The possibilities are endless! This doesn’t even cover the fact that you can use an arrow function as a parameter in ANOTHER function, and that’s its own can of worms…