/ Gists / Smarter if
On gists

Smarter if

JavaScript Vue.js

code.js Raw #

// Noob
const eventHandler = e => {
  if (e.key === 'Escape') {
    this.closeGallery()
  }
  if (e.key === 'ArrowLeft') {
    this.prevMedia()
  }
  if (e.key === 'ArrowRight') {
    this.nextMedia()
  }
}

// Profi
const eventHandler = e => {
  const keyActions = {
    Escape: closeGallery,
    ArrowLeft: prevMedia,
    ArrowRight: nextMedia
  }

  keyActions[e.key]?.()
}