我有以下 C++ 结构,我想在 Javascript 中尽可能忠实地创建它:
struct Vertex
{
float coords[4];
float colors[4];
};
所以我做了以下事情:
function Vertex(coords, colors)
{
this.coords = [];
this.colors = [];
}
现在,下面的工作是创建一个 Vertex 实例:
var oneVertex = new Vertex();
oneVertex.coords = [20.0, 20.0, 0.0, 1.0];
oneVertex.colors = [0.0, 0.0, 0.0, 1.0];
但下面的(华而不实?)不是:
var oneVertex = new Vertex([20.0, 20.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 1.0]);
为什么?我是 Javascript 的新手,我所阅读的内容表明它应该没问题。很明显不是。了解我所缺少的东西会很有帮助。谢谢。
最佳答案
您需要使用传递给函数的参数才能使其工作,如:
function Vertex(coords, colors)
{
this.coords = coords || [];
this.colors = colors || [];
}
关于javascript - 初始化包含数组的 javascript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36682040/