Skip to content

Instantly share code, notes, and snippets.

@lakesare
Created April 7, 2018 18:56
Show Gist options
  • Save lakesare/74285c9c765409ace3560e882cfed9f3 to your computer and use it in GitHub Desktop.
Save lakesare/74285c9c765409ace3560e882cfed9f3 to your computer and use it in GitHub Desktop.
function Player(name){
this.name = name;
}
Player.prototype.onLoaded = () => {};
Player.prototype.on = function (eventName, callback) {
if (eventName === 'loaded') {
this.onLoaded = callback;
}
};
Player.prototype.load = function () {
console.log('Loading myself!!');
const data = 'got this data from ' + this.name;
this.onLoaded(data);
};
const players = [
new Player('Tony'),
new Player('Evgenia'),
new Player('Alex')
];
for (let i = 0; i <= 2; i++){
players[i].on('loaded', function (data) {
console.log(data + ' ___ ' + i);
});
}
setTimeout(() => players[0].load(), 3000);
setTimeout(() => players[1].load(), 0);
players[2].load();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment