我需要调用一个函数,将其名称指定为字符串。这是我到目前为止的内容:
var obj = window[type]();
类型是字符串。我正在用功能Wall()测试它:
Wall.prototype = new GameObject();
Wall.prototype.constructor = Wall;
function Wall() {
this.bounds.width = 50;
this.solid = true;
this.bounds.height = 50;
this.layer = 1;
this.bounds.setCenter(engine.gameView.center().add(75, 75));
this.render = function() {
engine.gameView.fillRect(this.bounds, new Color(0, 0, 0));
};
}
GameObject是它“扩展”的类。当我new Wall()
正常使用时(不带反射),它可以正常工作。但是,如果我使用反射,它将把Wall作为窗口输出(正如我在其构造函数中用console.log看到的那样)。当我不加反射地称呼它时,它作为墙壁输出。因为在使用反射时它说它是一个窗口,所以它说bounds是未定义的,因为那是GameObject的属性。我该怎么做才能解决此问题?
调用构造函数时,需要“ new”关键字。IE,
var obj = new window[type]();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句