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

标签 javascript c++ c

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

相关文章:

javascript - 在JS中访问导入模块中的私有(private)函数?

javascript - Rails 中需要类似的日期函数

关于非特化模板的成员模板特化的 C++ 标准段落

c++访问此指针内的字符串索引

c - 为什么我们不能通过简单地将值分配给新变量来复制文件描述符?

C - 头文件与函数

c# - 等待文件存在于 ASP.NET 中

javascript - 如何在不检查 Canvas 上的每个点的情况下获取 SVG 路径字符串中包含的所有点?

c++ - Windows 套接字接收标志

c - 如何将 C 中的多行宏与行尾的注释结合起来