Without knowing how some of the other code is, you are clearly using ES6 in places so this could be a little more ES6-y.
{
const keys = document.querySelectorAll('.key')
document.addEventListener('keydown', playSound)
document.addEventListener('transitionend', removeTransition)
function playSound(e) {
let audio = document.querySelector(`[audio-key="${e.keyCode}"]`),
key = document.querySelector(`[data-key="${e.keyCode}"]`)
if(!audio) return
audio.currentTime = 0
audio.play()
key.classList.add('playing')
}
function removeTransition(e) {
[].forEach.call(keys, (key) => {
key.classList.remove('playing')
})
}
}
Some of the ideas here.
Instead of an immediately invoked function, and to keep the global scope clear, you can place all your code in a simple block.
{ ....code... }
And because you were immediately invoking the function, I assume you aren’t calling it by name, so I just got rid of audioController()
altogether. All it really did is set up the two event listeners.
Set up the keys
const outside of the functions and pass it in to any functions that needs it. If you only need this variable inside one single function, then define it inside that function with a let
. Just depends how it’s used.
The ‘playSound’ function variables were changed to let
instead of const, as they will be recreated/new variables each time a sound is played. The idea of a const is not necessary here.
And as Martin said, the loop in removeTransition is kind of weird. I’m not even sure how that works, since you’re calling forEach on an empty array and not really even using the passed in element object.
In any case, I don’t know if my code is functional but it’s a little “cleaned up” in the ES6 way.
Another thing to keep in mind is to try and create functions that receive their inputs from the calling code, rather than generating or creating state within the function, nor creating effects or changing variables outside it.
To do that means you’d probably have to create a handful of additional functions with smaller tasks, but using pure functions are more testable and reusable that way.
You could use variables instead of functions like const audioController() = () => { ... }
. This alternative syntax may or may not have any benefit in terms of your entire app and how you code, and hoisting becomes a concern. But it can have certain benefits in some situations.
Anyway, good luck!