简体   繁体   English

创建对象数组Javascript

[英]Create array of objects Javascript

I created this Object with 3 properties: 我创建了具有3个属性的此对象:

Node = {
    name : "",
    isOkay : true,
    rotation : 0.0
}; 

How would i go creating an array of these objects, in size of 100. 我将如何创建这些对象的数组,大小为100。

So later i could do something like this: 所以以后我可以做这样的事情:

nodeList[74].name = "Peter";

nodeList[74].isOkay = false;

nodeList[74].rotation = 1.3;

or similar... 或类似...

I'm really new to this, i found couple of topics about this, but it never compiles properly. 我真的很陌生,我找到了一些与此相关的主题,但是它永远无法正确编译。

I would be really grateful if anyone could show the proper syntax, Thanks! 如果有人可以显示正确的语法,我将非常感谢,谢谢!

I would use this way: 我会用这种方式:

var Node = function() {
    this.name = "";
    this.isOkay = true;
    this.rotation = 0.0
}

var nodeList = [];
for (var i = 0; i < 10; i++) 
{ 
    nodeList.push(new Node()); 
}

nodeList[0].name = "test";

So you could create a new object(really new) in order to manage it later. 因此,您可以创建一个对象(确实是对象)以便以后进行管理。 Look here . 这里

EDIT : 编辑

What I have done is created an object with a constructor method, you can check it on MDN here . 我所做的是使用构造函数方法创建了一个对象,您可以在MDN上进行检查。

Creating an object like you have done: 像创建对象一样创建对象:

var Node = { /* ... */ }

Is like having one object initiated. 就像启动一个对象一样。 To have another, you'll have to write another one and so on. 要拥有另一个,则必须编写另一个,依此类推。 With that contructor you may create any instances you want based on that model . 使用该构造函数,您可以基于该模型创建所需的任何实例。

You might want to do this lazily 您可能想懒惰地这样做

Depending on the situation might be helpful to do this lazily 视情况而定,可能会延迟执行此操作

var Node = function(name, isOkay,rotation){
    if(!(this instanceof Node)) return new Node(name,isOkay,rotation);
    else {
        this.name = name;
        this.isOkay = isOkay;
        this.rotation = rotation;
    }
}

var NodeCollective = function(numberOfNodes){
        if(!(this instanceof NodeCollective)) return new NodeCollective(numberOfNodes);
        else{
                var _collective={};
                var _defaultName = "", _defaultIsOkay = true, _defaultRotation=0.0;
                this.length = numberOfNodes;
                this.getNode=function(nodeNumber){
                    if(!_collective.hasOwnProperty(nodeNumber) && nodeNumber < numberOfNodes){
                        _collective[nodeNumber]=
                            Node(_defaultName,_defaultIsOkay,_defaultRotation);
                    }
                    //I am just assuming I am not going to get garbage
                    //you can put in checks to make sure everything is kosher
                    //if you really want to
                    return _collective[nodeNumber];
                };
        }
}

but it also depends on what you are trying to do... if you might not be getting all of the nodes in your program then implementing them in some way that avoids greedily creating them could save you a lot of time if the code is executed often, but if the piece of code isn't executed often this might be over kill. 但这还取决于您要尝试执行的操作...如果您可能未在程序中获取所有节点,则以某种避免贪婪创建它们的方式实现它们可以在执行代码后为您节省大量时间通常,但是如果不经常执行这段代码,可能就太过致命了。

var nodeList = [];          // create empty array
nodeList.push(Node);        // add the object to the end of the array
nodeList[0].rotation = 1.3; // set rotation property of the object
console.log(nodeList[0]);   // prints the object to console

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

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