初始化包含数组的javascript对象

1 javascript arrays object

我有以下C++结构,我想在Javascript中尽可能忠实地创建:

struct Vertex
{
   float coords[4];
   float colors[4];
};
Run Code Online (Sandbox Code Playgroud)

所以我做了以下事情:

function Vertex(coords, colors)
{
   this.coords = [];
   this.colors = [];
}
Run Code Online (Sandbox Code Playgroud)

现在,以下工作来创建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];
Run Code Online (Sandbox Code Playgroud)

但以下(slicker?)不会:

var oneVertex = new Vertex([20.0, 20.0, 0.0, 1.0], 
                            [0.0, 0.0, 0.0, 1.0]);
Run Code Online (Sandbox Code Playgroud)

为什么?我是Javascript的新手,我读过的内容很少表明它应该没问题.显然不是.了解我所缺少的内容会很有帮助.谢谢.

Sud*_*oti 5

你需要使用传递给函数的参数来使它工作,如:

function Vertex(coords, colors)
{
   this.coords = coords || [];
   this.colors = colors || [];
}
Run Code Online (Sandbox Code Playgroud)