0

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();
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
Moody_jr
  • 55
  • 7
  • 1
    What does it mean you "can't create an array in OOP Javascript" ? Is it a special rule you're following ? – ttzn Jun 24 '15 at 09:54
  • Btw in JS everything is an object, so in theory all JS is Object base. Arrays are object you can just create them like you normally do in JS and it's OOP. – GillesC Jun 24 '15 at 09:56

1 Answers1

6

I'm not entirely sure what you're asking, but if you want an array of Robots and to call sePresenter on each of them, you can use an array initializer ([...]) and Array#forEach (or there are several other ways to loop arrays):

var robots = [
    new Robot('Maurice',95,2,(5,8)),
    new Robot('Lilian',76,3,(12,25)),
    new Robot('Ernest',100,1,(11,14)),
    new Robot('Juliette',87,3,(2,17))
];
robots.forEach(function(robot) {
    robot.sePresenter();
});

Side note: Your calls to your constructors are a bit odd. This:

new Robot('Juliette',87,3,(2,17));

...is exactly the same as this:

new Robot('Juliette',87,3,17);
// Note no (2,...) -------^

...since both 2 and 17 are literals.

In JavaScript, the comma operator evaluates both of its operands, and its result is the second operand's value. So (2,17) evalutes to 17. The parentheses only call a function when the thing in front of the parens is a function reference.

If the 2,17 is meant to be coordinates, you probably wanted an array:

new Robot('Juliette',87,3,[2,17]);
// [] rather than () -----^----^

...which you would reference within Robot as this.position[0] and this.position[1].

Or an object:

new Robot('Juliette',87,3,{x:2,y:17});
// Object initializer ----^^^^^^^^^^

...which you would reference within Robot as this.position.x and this.position.y.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875