Let's say I want class Warrior in my game. And my warriors can only walk.
class Warrior{
constructor(){}
walk(){
alert ('I can walk!');
}
}
x.walk(); //'I can walk!'
x.fight(); //error
Then I decided to create a weapon class so that when this class is active, my Warriors in the game can fight. Some pseudocode how I imagine it:
class Weapon{
canFight(type:Warrior){
set warrior.fight=()=>{alert('I can fight!')};}
}
let x=new Warrior();
x.walk(); //'I can walk!'
x.fight() //'I can fight!'
So I need to extend the class itself and all its instances with the new methods and parameters when some sort of magic extention code is present. Thus I can incapsulate behaviour in the separate classes and extend it to other classes without need to have noticed them.
What I saw is mixins but the idea behind that is to explicit change my Warrior class to incapsulate new functions. I can't just say that since now my warriors can fight, I need to change the the cases when i use Warriors to some sort of new type - FighterWarriors and that can be a real pain if I need to quickly enchance the objects with the new behaviour.
This is working technique in c# and swift but I don't know about other languages.
So the questions are: how can I make this type of behaviour in Typescript? If can't, does the pure JS supports this? What can I read additionally for this theme?