I'm doing a new personal exercise and I'm beginning JS OOP.
My goal is: Creating a little robot army where each robot introduce themselves.
My code is great and it works, but I would like to improve it. I would like to add my robots in an array and create a loop for the introduction of each robot.
it's not difficult but I can't create an array in OOP Javascript. I don't understand how can I create one with all features of my robots.
Here is my code:
// Objet Robot
function Robot(nick, pv, maxSpeed, position) {
this.nick = nick;
this.pv = pv;
this.maxSpeed = maxSpeed;
this.position = position;
};
//Méthode présentation des robots
Robot.prototype.sePresenter = function() {
console.log("Bonjour je m'appelle " + this.nick + ". J'ai " + this.pv + " points de vie." + " Je me déplace à " + this.maxSpeed + " cases par seconde. Je suis à la case de coordonnées " + this.position);
};
// Variables
var robot1 = new Robot('Maurice',95,2,(5,8));
var robot2 = new Robot('Lilian',76,3,(12,25));
var robot3 = new Robot('Ernest',100,1,(11,14));
var robot4 = new Robot('Juliette',87,3,(2,17));
// Appel Méthode sePresenter
robot1.sePresenter();
robot2.sePresenter();
robot3.sePresenter();
robot4.sePresenter();