在javascript对象方法中从变量创建新对象

时间:2014-08-24 12:06:34

标签: javascript object

我正在尝试通过变量内部对象方法创建对象。像这样:

    ObjectFactory = function(){

    }
    ObjectFactory.prototype.createObject = function(objectName){
        return new objectName;
    }

    var of = new ObjectFactory();
    var MyObject = of.createObject('MyObjectClassName');

知道该怎么做吗?

2 个答案:

答案 0 :(得分:1)

只需删除引号(并在var之后添加空格)

var MyObject = of.createObject(MyObjectClassName);

(如果真的想要使用字符串,请参阅下文。)

完整示例: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>

如果你真的需要在那里使用字符串而不是函数引用,那么除非你想使用eval(并且通常最好避免它),你需要一个对象来查找构造函数on(例如,从字符串名称获取构造函数)。

如果构造函数是 globals ,你可以在全局对象上查找它们,但是全局变量是个坏主意。相反,给自己一个地图对象并将构造函数设置为地图的属性:

var constructorMap = {
    MyObjectClassName: MyObjectClassName
};

你甚至可以把它放在`ObjectFactory:

ObjectFactory.constructorMap = {
    MyObjectClassName: MyObjectClassName
};

然后

ObjectFactory.prototype.createObject = function(objectName){
    return new ObjectFactory.constructorMap[objectName];
};

完整示例: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>

答案 1 :(得分:1)

作为参数传递给ObjectFactory.prototype.createObject的内容必须具有类型function,您不能将字符串或其他类型传递给此函数。因为在该函数中,您已将input参数作为构造函数调用。以下代码可以没问题:

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);

用法示例:

var array = of.createObject(Array);
array.push(1);