最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Define a "nested" object constructor in JavaScript? - Stack Overflow

programmeradmin3浏览0评论

Is it possible to define an object within another 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:

var myEmbeddedObj = new MyObj.EmbeddedObj();

Meme for bonus points: Objectception! :o

Is it possible to define an object within another 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:

var myEmbeddedObj = new MyObj.EmbeddedObj();

Meme for bonus points: Objectception! :o

Share Improve this question edited Apr 7, 2012 at 22:46 user166390 asked Apr 7, 2012 at 2:06 Elliot BonnevilleElliot Bonneville 53.3k23 gold badges100 silver badges124 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 8

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 ment).

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. (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.

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. 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"));
发布评论

评论列表(0)

  1. 暂无评论