Javascript:实例化对象数组的最佳方式

时间:2015-07-05 23:21:07

标签: javascript arrays javascript-objects

使用构造函数创建大量对象来填充数组最简单的方法是什么?

我有一个构造函数而不是制作电视和电影对象:

var ms = new MemoryStream();
var rWriter = new ResourceWriter(ms);

rWriter.AddResource("key1", "value1");
rWriter.AddResource("key2", "value2");
rWriter.AddResource("key3", "value3");

rWriter.Generate();
rWriter.Close();

我正在制作许多我想放入数组的对象。这不起作用:

function Media(name, boxCover) {
    this.name = name;
    this.boxCover = boxCover;
};

我也尝试将var table = [ var avengers = new Media("avengers",'../assets/boxcovers/avengers.jpg'); var blade_runner = new Media("blade_runner",'../assets/boxcovers/blade_runner.jpg'); var brave = new Media("brave",'../assets/boxcovers/brave.jpg'); var catching_fire = new Media("catching_fire",'../assets/boxcovers/catching_fire.jpg'); var django = new Media("django",'../assets/boxcovers/django.jpg'); var finding_nemo = new Media("finding_nemo",'../assets/boxcovers/finding_nemo.jpg'); ]; 放在每行的开头。我真的需要像这样在数组中再次列出的每个对象,否则我可以节省重复的工作:

table.push(

5 个答案:

答案 0 :(得分:5)

这个的语法是

var table = {
    mykey: myproperty,
    mykey2: myproperty2
};

如果您希望变量是一个对象,那么您可以通过调用f.e table.avengers来访问属性,您的声明应如下所示:

var table = {
    avengers: new Media("avengers",'../assets/boxcovers/avengers.jpg'),
    blade_runner: new Media("blade_runner",'../assets/boxcovers/blade_runner.jpg'),
    brave: new Media("brave",'../assets/boxcovers/brave.jpg')
    //... and so on and so forth
};

如果您只想循环浏览它们,可以将其创建为数组:

var table = [
    new Media("avengers",'../assets/boxcovers/avengers.jpg'),
    new Media("blade_runner",'../assets/boxcovers/blade_runner.jpg'),
    new Media("brave",'../assets/boxcovers/brave.jpg')
    //... and so on and so forth
];

答案 1 :(得分:1)

您使用的语法错误。试试这个:

function Media(name, boxCover) {
    this.name = name;
    this.boxCover = boxCover;
};

var table = [
 new Media('avengers','../assets/boxcovers/avengers.jpg'),
 new Media('blade_runner','../assets/boxcovers/blade_runner.jpg')         
];

答案 2 :(得分:0)

您可以使用简单的语法,例如:

var mediatable = [];
var entry = {
    name: {},
    boxCover: {}
};
mediatable.push({
    name: "avengers",
    boxCover: '../assets/boxcovers/avengers.jpg'
});
mediatable.push({
    name: "blade_runner",
    boxCover: '../assets/boxcovers/blade_runner.jpg'
});
alert(mediatable.length);

或使用对象推送它:

entry.name = "brave";
entry.boxCover = '../assets/boxcovers/brave.jpg';
mediatable.push(entry);
alert(mediatable.length + ":" + mediatable[1].name);

答案 3 :(得分:0)

function Media(name, boxCover) {
    this.name = name;
    this.boxCover = boxCover;
};

function MediaTable(data) {
    var table = {};
    data.forEach(function(el){
        table[el[0]] = new Media(el[0], el[1])
    })

    return table;
};

var tableData = [
        ["avengers", '../assets/boxcovers/avengers.jpg'],
        ["blade_runner", '../assets/boxcovers/blade_runner.jpg'],
        ["brave" , '../assets/boxcovers/brave.jpg'],
        ["catching_fire", '../assets/boxcovers/catching_fire.jpg'],
        ["django", '../assets/boxcovers/django.jpg'],
        ["finding_nemo", '../assets/boxcovers/finding_nemo.jpg']
    ],
    table = new MediaTable(tableData);

答案 4 :(得分:-1)

像这样重写 var table ,这就是全部所需:

var table = [
  new Media("avengers",'../assets/boxcovers/avengers.jpg'),
  new Media("blade_runner",'../assets/boxcovers/blade_runner.jpg'),
  new Media("brave",'../assets/boxcovers/brave.jpg'),
  new Media("catching_fire",'../assets/boxcovers/catching_fire.jpg'),
  new Media("django",'../assets/boxcovers/django.jpg'),
  new Media("finding_nemo",'../assets/boxcovers/finding_nemo.jpg')
];