Reputation: 83
In a game I'm making I am handling an inventory system by keeping inventory objects in an array. The objects are made using two different constructor functions based on item type. My question is, where should I include this code:
Out of the constructor function
array.push(new object(param));
In the constructor function
new Object(param);
function Object(param) {
this.param = param;
array.push(this);
}
It is quite obvious that inside the function would be more efficient, but I'm not sure that it won't create more problems later on. In theory, which way is used more.
Thanks!
Upvotes: 1
Views: 68
Reputation: 1444
Consider a future idea, which will require you to store this Objects in different array or not to store them at all. With constructor like your it can become troublesome.
Always try to break things into smaller steps. Of course you need to be aware that you can cross the line. Although, simplicity is better than complexity.
Possible solution: why adding object to some array can't be it's method? Thanks to this, if later on you change how Objects are stored, you will only need to rewrite your method (without making changes to code where it was called).
Anyway I have another small tip. Don't overthink while learning. If you make a bad decision, you will eventually learn why it was bad and what would be better. Then it will be a lot easier to remember and understand.
Upvotes: 1