I have two objects defining something similar (simplified for the question):
var firstObject = function(){ };
firstObject.prototype.doSomethingFirstObjectsDo();
var secondObject = function(){ };
secondObject.prototype.doSomethingSecondObjectsDo();
Next, I have an Object Manager that works as a kind of interface for my main application for creating objects:
var ObjectManager = function()
{
this.create = {
FIRST:firstObject,
SECOND:secondObject
};
};
ObjectManager.prototype.createObject = function(type)
{
return new this.create[type]();
};
Finally, an example of a main application that uses the object manager to dynamically create either firstObjects or secondObjects:
var MainApplication = function(options)
{
this.objectTypes = options.objectTypes;
this.objManager = new ObjectManager();
};
MainApplication.prototype.createObjects = function()
{
for (var type in this.objectTypes)
{
var dynamicallyCreatedObject = this.objManager.createObject(type);
}
};
This approach works fine, but it has one drawback that I can see is that you need to formally define the name of the constructor function for each “Type” object that can be created.
, " ", , " " ObjectManager.
ObjectManager "" :
for (var type in this.objectTypes)
{
var dynamicallyCreateObject = new [type]();
};
- , JavaScript "" ?
:
, .
(function(){
$(document).ready(function(){
mainApp = window.mainApp = new MainApplication(options);
});
});
@casablanca: , , , NameSpace , . , , , , "" - , .