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

Good way to init array of JavaScript objects and their properties? - Stack Overflow

programmeradmin0浏览0评论

I know I can init an array of JS objects like this:

var things = [
  {
    prop1: 'foo',
    prop2: 'bar'
  },
  {
    prop1: 'foo',
    prop2: 'bar'
  }
];

I guess I would call these 'anonymous' types (sorry, I'm using C#/.NET language).

What if I want these to be of the same prototype? So I have defined a constructor:

var Thing = function Thing() {
};

Thing.prototype.prop1 = 'default value';
Thing.prototype.prop2 = 'default value';

Now I want both the items in my original code above to be Things. Is there a good way to do this?

If I were to guess, I would say maybe something like:

var things = [
  new Thing() {
    prop1: 'foo',
    prop2: 'bar'
  },
  new Thing() {
    prop1: 'foo',
    prop2: 'bar'
  }
];

Which is basically C# object initializer syntax. What I'm trying to avoid is:

var thing1 = new Thing();
thing1.prop1 = 'foo';
thing1.prop2 = 'bar';
var thing2 = new Thing();
thing2.prop1 = 'foo';
thing2.prop2 = 'bar';
var things = [thing1, thing2];

Edit:

I should also note that my prototypes also contain some functions that are shared. Also in actuality I have arrays nested 3 deep, so its something more like:

{
   [
    { 
      [
        {},
        {}
      ]
    },
    {
      [
        {},
        {}
      ]
    }
  ]
}

Which is why I as hoping to just init everything inline like this and not setting each property line by line.

I know I can init an array of JS objects like this:

var things = [
  {
    prop1: 'foo',
    prop2: 'bar'
  },
  {
    prop1: 'foo',
    prop2: 'bar'
  }
];

I guess I would call these 'anonymous' types (sorry, I'm using C#/.NET language).

What if I want these to be of the same prototype? So I have defined a constructor:

var Thing = function Thing() {
};

Thing.prototype.prop1 = 'default value';
Thing.prototype.prop2 = 'default value';

Now I want both the items in my original code above to be Things. Is there a good way to do this?

If I were to guess, I would say maybe something like:

var things = [
  new Thing() {
    prop1: 'foo',
    prop2: 'bar'
  },
  new Thing() {
    prop1: 'foo',
    prop2: 'bar'
  }
];

Which is basically C# object initializer syntax. What I'm trying to avoid is:

var thing1 = new Thing();
thing1.prop1 = 'foo';
thing1.prop2 = 'bar';
var thing2 = new Thing();
thing2.prop1 = 'foo';
thing2.prop2 = 'bar';
var things = [thing1, thing2];

Edit:

I should also note that my prototypes also contain some functions that are shared. Also in actuality I have arrays nested 3 deep, so its something more like:

{
   [
    { 
      [
        {},
        {}
      ]
    },
    {
      [
        {},
        {}
      ]
    }
  ]
}

Which is why I as hoping to just init everything inline like this and not setting each property line by line.

Share Improve this question edited Oct 7, 2012 at 23:52 CodingWithSpike asked Oct 7, 2012 at 23:44 CodingWithSpikeCodingWithSpike 43.7k18 gold badges105 silver badges139 bronze badges 0
Add a comment  | 

4 Answers 4

Reset to default 13
var Thing = function(params) {
  this.prop1 = params.prop1;
  this.prop2 = params.prop2;
};

var things = [
  new Thing({
    prop1: 'foo',
    prop2: 'bar'
  }),
  new Thing({
    prop1: 'foo',
    prop2: 'bar'
  }),
];

You are not making use of your 'constructor'. Its preferred to initialize values IN YOUR CONSTRUCTOR:

var Thing = function Thing(prop1, prop2) {
    this.prop1 = prop1;
    this.prop2 = prop2;
};

and then do:

var thing1 = new Thing("foo", "bar");
var thing2 = new Thing("foo", "bar");

In this cases I add a "config" method to the object:

function Thing() {
}
Thing.prototype.prop1 = 'foo';
Thing.prototype.prop2 = 'bar';
Thing.prototype.config = function(data) {
    for (var i in data)
        if (data.hasOwnProperty(i))
            this[i] = data[i];
}

var array = [
    new Thing().config({
        prop1: 'foobar',
        prop2: 'barfoo'
    })
];

The following may work, is this something you'd want to avoid also?

var thing1 = new Thing();
var thing2 = new Thing();

thing1.prototype = {
    prop1: "foo",
    prop2: "bar"
};

thing2.prototype = {
    prop1: "foo",
    prop2: "bar"
};

Another thing I can think of is making the constructor yourself so it allows something like the following:

var things = [
    new Thing({
        prop1: "foo",
        prop2: "bar"
    }),
    new Thing({
        prop1: "foo",
        prop2: "bar"
    })
];
发布评论

评论列表(0)

  1. 暂无评论