Jason Basanese Jason Basanese - 7 months ago 14
Javascript Question

How to set a class method equal to multiple functions? - Javascript

How would one do something like the following:

var turret = new Turret();
Turret.prototype.fire = shootMissiles("atAirUnits"),
shootMachineGun("atInfantry"),
setReload();

turret.fire();


I want know how to make it so that a call to
turret.fire();
calls all of these functions with their given parameters. Keep in mind the
setReload
function needs to still have access to
this.reloadTime
from the Turret class. I understand function composition to a certain extent, but with that the result of one of the functions is passed as an argument to the next which I do not want.
Further Explanation Any of these functions such as shootMissiles could take different or more arguments, such as "atTanks" or "atRandom". Also any other class such as rocketSoldier could use shootMissiles function in its fire function with different parameters. Another function such as shootMissiles might need access to Turret class variables such as this.missileSize. If I have been unclear, please comment with how I should elaborate.

GG. GG.
Answer

You are looking for:

Turret.prototype.fire = function () {
    shootMissiles("atAirUnits");
    shootMachineGun("atInfantry"); 
    setReload();
};
Comments