Lately, on a number of js game making blogs and sites etc. I have seen a new convention for creating some kind of "instance". It looks like this:
var newCharacter = function (x, y) {
return {
x: x,
y: y,
width: 50,
height: 50,
update: function () {
//do update stuff here
}
};
};
var myCharacter = newCharacter(20, 30); //create "instance"
As opposed to the the traditional way:
var Character = function (x, y) {
this.x = x;
this.y = y;
this.width = 50;
this.height = 50;
this.update = function () {
//do update stuff here
};
};
var myCharacter = new Character(20, 30); //create intance
I was curious what the advantage of using the first way might be. Is it more efficent, semantic, or faster?