javascript 从变量创建新的javascript对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5838898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
create new javascript object from variable
提问by yee379
i would like to create a new object in javascript (using simple inheritance) such that the class of the object is defined from a variable:
我想在 javascript 中创建一个新对象(使用简单继承),以便从变量定义对象的类:
var class = 'Person';
var inst = new class
any ideas?
有任何想法吗?
回答by meder omuraliev
You can do something like
你可以做类似的事情
function Person(){};
var name = 'Person';
var inst = new this[name]
The key is just referencing the object that owns the function that's the constructor. This works fine in global scope but if you're dumping the code inside of a function, you might have to change the reference because this
probably wont work.
关键只是引用拥有构造函数的对象。这在全局范围内工作正常,但如果您在函数内转储代码,则可能必须更改引用,因为this
可能无法正常工作。
EDIT: To pass parameters:
编辑:传递参数:
function Person(name){alert(name)};
var name = 'Person';
var inst = new this[name]('john')
回答by artfulrobot
Here's how I do it. Similar to meder's answer.
这是我如何做到的。类似于meder的回答。
var className = 'Person'
// here's the trick: get a reference to the class object itself
// (I've assumed the class is defined in global scope)
var myclass = window[className];
// now you have a reference to the object, the new keyword will work:
var inst = new myclass('params','if','you','need','them');
回答by KyleM
To improve on the answer above by meder:
要改进 meder 上面的答案:
To invoke using a function:
使用函数调用:
You would simply replace 'this' with the scope containing the class you are referring to, for example if the class is written in the global scope like below:
您只需将 'this' 替换为包含您所指类的范围,例如,如果该类是在全局范围内编写的,如下所示:
function Person(name){alert(name)};
var name = 'Person';
function classMaker(){
var inst = new window[name]('john');
}
classMaker();
回答by eeggen
I had a similar problem in Node.js, when I had to dynamically create different handler objects depending on the data. First I gathered all available handlers, which were implemented as separate modules, in an object. In a simple case this could be hardcoded like this.
我在 Node.js 中遇到了类似的问题,当我不得不根据数据动态创建不同的处理程序对象时。首先,我在一个对象中收集了所有可用的处理程序,这些处理程序作为单独的模块实现。在一个简单的情况下,这可以像这样硬编码。
var handlers = {
handlerX : require('HandlerX'),
handlerY : require('HandlerY')
};
Then, whenever I had to instantiate a handler, I did it like this:
然后,每当我必须实例化处理程序时,我都是这样做的:
var handlername = getHandlerName()
var handler = new handlers[handlername]();
Of course this only works if you know, or can programmatically determine the list of all objects you will need to create.
当然,这只有在您知道或可以通过编程确定您需要创建的所有对象的列表时才有效。
回答by Homer6
This talk was really helpful when I got into different inheritance patterns in javascript. Example code is included in the second link (the video introduces it).
当我进入 javascript 中的不同继承模式时,这个演讲真的很有帮助。示例代码包含在第二个链接中(视频介绍了它)。
http://alexsexton.com/inheritance/demo/