I want to separate the creation of JavaScript objects from the code used so that I have the flexibility to replace one implementation of the object with another implementation of the object that has the same signature, without touching most of the code. To do this, I come up with the concept of a repository and the Factory method for creating objects. Here is the implementation:
function ObjectFactory() {}
ObjectFactory.create = function (o) {
var args = [].slice.call(arguments, 1);
function F() {}
F.prototype = o.prototype;
var instance = new F();
o.apply(instance, args);
return instance;
};
var Repository = {
'invitation': Invitation,
'message': Message
};
var inv = ObjectFactory.create(Repository["invitation"], "invitation", "invitation body", "Sender");
var msg = ObjectFactory.create(Repository["message"], "message", "message body");
var inv2 = ObjectFactory.create(Repository["invitation"], "invitation2", "invitation body2", "Sender");
, , , - ( , - 5-10 200 1000 ) . JavaScript, , . , ES5 Object.create, IE8 FF3.6.