简体   繁体   English

在 JavaScript 中定义一个“嵌套”的 object 构造函数?

[英]Define a "nested" object constructor in JavaScript?

Is it possible to define an object within another object?是否可以在另一个 object 中定义一个 object? I'm thinking something like this:我在想这样的事情:

function MyObj(name) {
    this.name = name;

    function EmbeddedObj(id) {
        this.id = id;
    }
}

And I could then create an EmbeddedObj like this:然后我可以像这样创建一个 EmbeddedObj:

var myEmbeddedObj = new MyObj.EmbeddedObj();

Meme for bonus points: Objectception: :o奖励积分的表情包:Objectception: :o

Yes, and no.是的,没有。

function MyObj(name) {
    this.name = name;
}
MyObj.EmbeddedObj = function EmbeddedObj(id) {
    this.id = id;
}
new MyObj.EmbeddedObj(42);

Would run, but it might not yield the expected results for "embedded object" (see comment).会运行,但它可能不会产生“嵌入式对象”的预期结果(见评论)。

Note that in the case of new expr the expression is evaluated first so, in this case it creates a new object using the function-object evaluated from MyObject.EmbeddedObj as a constructor.请注意,在new expr的情况下,表达式首先被评估,因此,在这种情况下,它使用从MyObject.EmbeddedObj评估的函数对象作为构造函数创建一个新的 object。 (There is a silly rule with parenthesis in the expression, but that's another story.) (表达式中有一条带括号的愚蠢规则,但那是另一回事了。)


Now, if a "parent" and "child" relationship was desired, that could be done, using a more round-about method:现在,如果需要“父”和“子”关系,可以使用更迂回的方法来实现:

function Parent (name) {
   this.name = name;
   var parent = this; // for closure
   this.Child = function Child () {
      this.Parent = parent;
   }
}

// create new parent object
var parent = new Parent();       
// each new parent has a different Child constructor and
// any function-object can be used as a constructor
var child = new parent.Child();
// true: child is "bound" to parent
child.Parent === parent;
function MyObj(name) {
    this.name = name;
}

MyObj.EmbeddedObj = function(id) {
    this.id = id;
}

var myEmbeddedObj = new MyObj.EmbeddedObj();

Does that look like what you're after?这看起来像你想要的吗?

Here is example of nested constructor.这是嵌套构造函数的示例。

function cimdb(name,review,year) {

 function nestedConstructor(name,review,year) {
    this.name = name;
    this.review = review;
    this.year = year
};

    this.name = name;
    this[name] = new nestedConstructor(name,review,year);

}



  var lionking = new cimdb("The Lion King", "The lion King review ..", 2015);

I guess this is what you mean by nested object constructor.我想这就是嵌套 object 构造函数的意思。

The easiest way to nest other objects in a constructor is to create its field and then create a new object when invoking the constructor.在构造函数中嵌套其他对象的最简单方法是创建它的字段,然后在调用构造函数时创建一个新的 object。 Below is an example:下面是一个例子:

function Product(name, price, category, producer) {
    this.name = name;
    this.price = price;
    this.category = category;
    // nested constructor
    this.producer = producer;
}

function Producer(contributor, address) {
    this.contributor = contributor;
    this.address = address;
}

let prod1 = new Product("Milk", 2.5, "Dairy", new Producer("Nestle", "Warszawa"));

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

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