假设我在 C 中有以下内容
struct address{
char name;
int id;
char address;
};
struct address adrs[40]; //Create arbitrary array of the structure. 40 is example
adrs[0].name = 'a';
id[0] = 1;
...
定义和创建用户定义结构数组的等效方法是什么。
谢谢
最佳答案
如果您要为对象预定义布局,您可能希望使用构造函数样式的函数。
function address() {
this.name = null;
this.id = null;
this.address = null;
}
数组没有类型,您不必指定长度。
var adrs = [];
你可以像这样创建一个新的address
实例
var item = new address(); // note the "new" keyword here
item.name = 'a';
item.id = 1;
// etc...
然后您可以将新项目推送
到数组中。
adrs.push(item);
或者您可以从数组中添加一个新项目,然后通过索引器访问它。
// adrs has no items
adrs.push( new address() );
// adrs now has 1 item
adrs[0].name = 'a';
// you can also reference length to get to the last item
adrs[ adrs.length-1 ].id = '1';
关于javascript - Javascript 中等效的 C/C++ 数据结构是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3999957/