I have a question maybe a little stupid, but come on, in Javascript, I can create an instance of an object stating thus, let's go in an example of a game, where I have to install bullets to shoot at the enemy:
var Bala = {
myFunctionInit: function myFunctionInit(){
console.log("Pul!");
},
x = 0,
y = 0,
speed = 0
};
But then I want to be able to instantiate several of these bullets, however I can not do something like:
var outraBala = new Bala;
and I understand why I can not, because the Bullet is already an object instance, this I could see giving a "console.log (Bullet);", the question is, how can I instantiate the Bullet, correctly declaring objects This way?
I know what I can do:
var outraBala = Object.assign({}, Bala);
I also thought of putting the Bullet literal object as the return of a function, so I could instantiate it like this:
var Bala = function(x, y, speed) {
return = {
myFunctionInit: function myFunctionInit(){
console.log("Pul!");
},
x: x,
y: y,
speed: speed
};
}
var outraBala = Bala(1, 1, 10);
But I'm not sure if it's the best way to handle this situation.
obs: I do not want to instantiate objects using class, this way I know how to do it, I want to be able to create them in this declarative way (which I personally think is the clearest of all) and instantiate them in the best possible way , and if it's stupid of me, I need to understand why.