I was wondering whether this would be a correct way to initialize an object, which requires a random position and speed on the map. I am not sure whether I am violating the encapsulation principle with the object initializers this.x and thi.y by calling Warrior.chooseX() and Warrior.choosey(), respectively.
var Warrior = function() {
this.x = Warrior.chooseX();
this.y = Warrior.chooseY();
this.speed = Warrior.chooseSpeed();
};
Warrior.chooseX = function() {
return Math.random() * 600 - 200;
};
Warrior.chooseY = function() {
var randomNumber = Math.random();
if (0 <= randomNumber && randomNumber < 0.5) {
return 100;
} else {
return 200;
}
};
Warrior.chooseSpeed = function() {
return Math.floor(Math.random() * 200) + 100;
};
var warrior = new Warrior();
Thanks for any suggestion.