gpt4 book ai didi

javascript - Javascript 中等效的 C/C++ 数据结构是什么?

转载 作者:搜寻专家 更新时间:2023-10-31 00:21:53 25 4
gpt4 key购买 nike

假设我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com