JSFiddle - React, Tailwind, and code Playground
JavaScript
function Engine() {
this.balance = 0;
this.externalBalanceListener;
this.registerBalanceListener = function(l) {
this.externalBalanceListener= l;
this.externalBalanceListener(this.balance);
};
this.getBalance = function() {
return this.balance;
};
this.setBalance = function (newBal) {
if (newBal != this.balance) {
this.balance = newBal;
if (this.externalBalanceListener != undefined) {
this.externalBalanceListener(newBal);
}
}
};
};
function DemoEngine() {
this.startGame = function(stake, numLines) {
var won;
setBalance(this.getBalance() - stake*numlines);
won = Math.round(Math.random() * 10) * Stake;
this.setBalance(this.getBalance() + won);
return won;
};
}
DemoEngine.prototype = Engine;
function DemoGame() {
function balanceListener(balance) {
console.log(balance);
}
var engine = new DemoEngine();
engine.registerBalanceListener(balanceListener); // This throws an exception: Uncaught TypeError: Object [object Object] has no method 'registerBalanceListener'
engine.startGame(10, 25);
}
var game = new DemoGame();