Reputation: 2624
I'm trying to create object by variable inside object method. Something like this:
ObjectFactory = function(){ } ObjectFactory.prototype.createObject = function(objectName){ return new objectName; } var of = new ObjectFactory(); var MyObject = of.createObject('MyObjectClassName');
Any idea how to do it?
Upvotes: 0
Views: 64
Reputation: 29285
What you pass to ObjectFactory.prototype.createObject
as argument must have type function
, you can't pass string or other types to this function. Because in that function you've invoked the input argument as a constructor function. The following codes could be fine:
ObjectFactory.prototype.createObject = function(objectName){
return new objectName;
}
/* This function is going to be a constructor function */
function MyObjectClassName {
this.properties = /* some value */;
this.methods = /* some functions */
}
var of = new ObjectFactory();
var MyObject = of.createObject(MyObjectClassName);
Usage example:
var array = of.createObject(Array);
array.push(1);
Upvotes: 1
Reputation: 1074028
Just remove the quotes (and add a space after var
):
var MyObject = of.createObject(MyObjectClassName);
(See below if you really wanted to use a string.)
Full Example: Live Copy
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example</title>
<style>
body {
font-family: sans-serif;
}
</style>
</head>
<body>
<script>
(function() {
"use strict";
var ObjectFactory = function(){
};
ObjectFactory.prototype.createObject = function(ctor){
return new ctor;
};
function Foo() {
}
var of = new ObjectFactory();
var f = of.createObject(Foo);
display("<code>f instanceof Foo</code>? " + (f instanceof Foo));
function display(msg) {
var p = document.createElement('p');
p.innerHTML = String(msg);
document.body.appendChild(p);
}
})();
</script>
</body>
</html>
If you really need to be using a string there rather than a function reference, then unless you want to use eval
(and it's usually best avoided), you need an object to look up the constructor function on (e.g., to get the constructor function from the string name).
If the constructor functions are globals, you can look them up on the global object, but globals are a bad idea. Instead, give yourself a map object and set up the constructors as properties of the map:
var constructorMap = {
MyObjectClassName: MyObjectClassName
};
You could even put this on `ObjectFactory:
ObjectFactory.constructorMap = {
MyObjectClassName: MyObjectClassName
};
then
ObjectFactory.prototype.createObject = function(objectName){
return new ObjectFactory.constructorMap[objectName];
};
Full Example: Live Copy
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example</title>
<style>
body {
font-family: sans-serif;
}
</style>
</head>
<body>
<script>
(function() {
"use strict";
function Foo() {
}
var ObjectFactory = function(){
};
ObjectFactory.constructorMap = {
Foo: Foo
};
ObjectFactory.prototype.createObject = function(objectName){
return new ObjectFactory.constructorMap[objectName];
};
var of = new ObjectFactory();
var f = of.createObject("Foo");
display("<code>f instanceof Foo</code>? " + (f instanceof Foo));
function display(msg) {
var p = document.createElement('p');
p.innerHTML = String(msg);
document.body.appendChild(p);
}
})();
</script>
</body>
</html>
Upvotes: 1