简体   繁体   English

如何动态实例化javascript中的类?

[英]How do I dynamically instantiate a class in javascript?

I'm starting out with classes in Javascript and have hit a wall. 我从Java类开始,并且碰壁了。 I've looked all over for a tutorial that goes a little further than simply how to construct a class (usually Animal) then extend the class and have a Method do something (Dog alert('Bark');). 我一直在寻找一个教程,该教程比简单地讲如何构建一个类(通常是Animal)然后扩展该类并让Method进行某些操作(Dog alert('Bark');)要走得更远。

I have created a class that I want a user to be able to instantiate (is that the right word)? 我创建了一个我希望用户能够实例化的类(这是正确的词)吗? For example the first stage in my program is for the user to give the class a name, and then start to populate the various variables in the class. 例如,在我的程序中,第一步是让用户给该类命名,然后开始填充该类中的各种变量。 When they've done that they may do it again many times. 当他们做完之后,他们可能会再次做多次。

EDITED, FULL EXAMPLE OF CODE: 编辑的完整示例代码:

function Blind() {
    this.Type = null;
    this.Colour = null;
    this.Width = 500;
    this.Drop = 500;
    this.Price = 0;
}

function BlindAluminium() {
    Blind.call(this);
    this.Type = 'aluminium';
    this.SubType = null;
    this.StackHeight = 0;
}

That is the code that defines the object. 那是定义对象的代码。

Here is the HTML that will allow the user to define a new object: 这是允许用户定义新对象的HTML:

<form id="create_blind_form" name="create_blind_form" method="post" action="">
    <label for="blind_name">Blind Name: <input name="blind_name" id="blind_name" type="text" /></label>
    <input name="submit" type="submit" value="Submit" />
</form>

On submitting that I need a new object to be created, in pseudo-code, something like this: 提交后,我需要使用伪代码创建一个新对象,如下所示:

var *blind_name_from_the_form* = new BlindAluminium();

Then later I hope to act on it like this: 然后,我希望对它采取以下措施:

*blind_name_from_the_form*.subType = '50mm';

The short answer is that you're going to have to use eval . 简短的答案是,您将必须使用eval If you want to call a function (or class) with a name known only at run-time, there's no way around it. 如果要使用仅在运行时知道的名称来调用函数(或类),则无法解决。

eval("var className = new MyObject();");

That being said, you may want to consider some alternatives. 话虽如此,您可能需要考虑一些替代方案。 People will give you all sorts of reasons why eval is bad, which are true. 人们会给您各种评估错误的原因,这是事实。 But to me, going down this route is just going to be awkward to manage. 但是对我来说,沿着这条路走只会很尴尬。 Yes, dig into Crockford. 是的,深入Crockford。

Is it possible to use Javascript's prototypical inheritance, instead of trying to use awkward classes? 是否可以使用Javascript的原型继承,而不是尝试使用笨拙的类? Given that classes mean nothing in Javascript, there's really no reason to use them if they don't fit the problem. 鉴于类在Javascript中没有任何意义,因此如果它们不适合问题,则实际上没有理由使用它们。

Maybe by storing the name inside another object? 也许通过将名称存储在另一个对象中?

var instances = {};
instances['theNameTheUserChose'] = new MyObject();

You are always going to have to have some sort of reference to your type. 您总是需要对您的类型进行某种形式的引用。 Lets say you create a constructor function (class) and call it MyObject. 假设您创建了一个构造函数(类)并将其命名为MyObject。 If I understand you correctly the use case is that your users should be able to do something like this instead: 如果我正确理解您的用例,那么您的用户应该可以执行以下操作:

var userObject = new SomethingElse(); // instead of new MyObject();

This is programming, so you are always going to have to be explicit. 这是编程,所以您总是必须明确。 You could do something like this: 您可以执行以下操作:

var types = {};
types["aType"] = MyObject;
var userObject = new types["aType"];

Note that even though you do like above, your users are going to have to know what the "aType" property of the "types" object contains. 请注意,即使您像上面那样,您的用户也将必须知道“类型”对象的“ aType”属性所包含的内容。 You can't really do something like this: 您实际上不能做这样的事情:

var userObject = guessTheClassName();

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM