I need to call inherited static function (from Tower class) which adds new tower to array, based on type of tower.
I want to call function Mine.buy() (where Mine is subclass and buy is the static function), that should add new Mine to the array.
When the static buy function is called by some class, then it adds that class object to the array.
Simplified code:
const towers = []; // Array, where all towers will be stored
// superclass/ parent class of other towers
class Tower {
static buyAble = false;
static buy() {
if (Tower.buyAble) towers.push(new Tower());
// I mean if it is possible do to something like tihs:
//if (CurrentTower.buyAble) towers.push(new CurrentTower());
// CurrentTower is class name of used class (like when Mine.buy(), then CurrentTower = Mine)
}
}
// Defining types of towers (subclasses)
class Base extends Tower {
static buyAble = true;
}
class Mine extends Tower {
static buyAble = false;
}
class Storage extends Tower {
static buyAble = true;
}
Tower.buy(); // Leaves the towers array empty, because is not buyAble -> []
Base.buy(); // Adds Base to towers array, because is buyAble -> [Base]
Mine.buy(); // Leaves the towers array as it was before -> [Base]
Storage.buy(); // Leaves the towers array as it was before -> [Base, Storage]
It should do the same think like this:
const towers = []; // Array, where all towers will be stored
// Defining types of towers (subclasses)
class Base {
static buyAble = true;
static buy() {
if (Base.buyAble) towers.push(new Base());
}
}
class Mine {
static buyAble = false;
static buy() {
if (Mine.buyAble) towers.push(new Mine());
}
}
class Storage {
static buyAble = true;
static buy() {
if (Storage.buyAble) towers.push(new Storage());
}
}
Base.buy(); // Adds Base to towers array, because is buyAble -> [Base]
Mine.buy(); // Leaves the towers array as it was before -> [Base]
Storage.buy(); // Leaves the towers array as it was before -> [Base, Storage]
Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW
Comments
Post a Comment