初始化 JavaScript 对象数组及其属性的好方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12773812/
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
Good way to init array of JavaScript objects and their properties?
提问by CodingWithSpike
I know I can init an array of JS objects like this:
我知道我可以像这样初始化一组 JS 对象:
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).
我想我会称这些为“匿名”类型(抱歉,我使用的是 C#/.NET 语言)。
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 Thing
s. Is there a good way to do this?
现在我希望上面原始代码中的两个项目都是Thing
s。有没有好的方法可以做到这一点?
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:
这基本上是 C# 对象初始值设定项语法。我试图避免的是:
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:
我还应该注意,我的原型还包含一些共享的功能。实际上,我的数组嵌套了 3 层深,所以它更像是:
{
[
{
[
{},
{}
]
},
{
[
{},
{}
]
}
]
}
Which is why I as hoping to just init everything inline like this and not setting each property line by line.
这就是为什么我希望像这样内联初始化所有内容而不是逐行设置每个属性。
采纳答案by Alcides Queiroz Aguiar
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'
}),
];
回答by Sidharth Mudgal
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");
回答by A. Matías Quezada
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'
})
];
回答by jeremy
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"
})
];