octavian-nita
1/25/2017 - 4:08 PM

Extending Function in ES2015 (see http://stackoverflow.com/questions/36871299/how-to-extend-function-with-es6-classes)

(function () {
  'use strict';

  const out = document.getElementById('out');
  function array(arrayLike) { return Array.prototype.slice.call(arrayLike); }
  function cl() { if (out) { out.textContent = ''; } }
  function pr() { if (out) { out.textContent += array(arguments).join(' ') + '\n'; } }
  function p() { if (out) { out.textContent += array(arguments).join('\n') + '\n'; } }

  class GameView extends Function {
    constructor() {
      super('...args', 'return this.__call__ && this.__call__(...args);');
      return this.bind(this);
    }
    __call__() {
      p('GameView::__call__');
      p('Rendering game...');
    }
  }

  class Game extends EventEmitter {
    constructor() {
      super();
      this.on('init', new GameView());
    }
    init() {
      p('Game::init');
      this.emit('init');
    }
  }

  new Game().init();
}());