Javascript 使用jQuery初始化类

Javascript 使用jQuery初始化类,javascript,jquery,barcode-scanner,Javascript,Jquery,Barcode Scanner,我正在尝试使用jquery和javascript检测条形码扫描仪输入。我发现这个类可以在stackoverflow中使用: class BarcodeScaner { initialize = () => { document.addEventListener('keypress', this.keyup) if (this.timeoutHandler) { clearTimeout(this.timeoutHandler) } this.

我正在尝试使用jquery和javascript检测条形码扫描仪输入。我发现这个类可以在stackoverflow中使用:

class BarcodeScaner {
  initialize = () => {
    document.addEventListener('keypress', this.keyup)
    if (this.timeoutHandler) {
      clearTimeout(this.timeoutHandler)
    }
    this.timeoutHandler = setTimeout(() => {
      this.inputString = ''
    }, 10)
  }

  close = () => {
    document.removeEventListener('keypress', this.keyup)
  }

  timeoutHandler = 0

  inputString = ''

  keyup = (e) => {
    if (this.timeoutHandler) {
      clearTimeout(this.timeoutHandler)
      this.inputString += String.fromCharCode(e.keyCode)
    }

    this.timeoutHandler = setTimeout(() => {
      if (this.inputString.length <= 3) {
        this.inputString = ''
        return
      }
      events.emit('onbarcodescaned', this.inputString)

      this.inputString = ''
    }, 10)
  }
}

但是,当我扫描条形码时,并没有收到警报。我也没有收到任何错误消息。

events.emit('onBarCodeScanned',this.inputString)
正在导致
未捕获引用错误:未定义事件(因为未定义
events
),您确定扫描仪已配置为发出键盘事件吗?
bc = new BarcodeScaner();

bc.initialize();

$(document).on( "onbarcodescaned", function(inputString) { 
  alert(inputString)
});