使用相同覆盖变量的javascript对象
我有一个我正在使用的javascript对象。我有一个verts数组,我想保持原样(称为verts的变量)。在对象中,我有一个setScalar()方法,该方法将采用此顶点矩阵并应用标量(这是一个简单的for循环乘以每个值)。当我创建一个新对象时,我应用标量,但当我创建一个新对象时,我不引用原始顶点数组,它似乎将标量应用于上一个对象中使用的缩放顶点 以下是verts阵列:使用相同覆盖变量的javascript对象,javascript,object,webgl,Javascript,Object,Webgl,我有一个我正在使用的javascript对象。我有一个verts数组,我想保持原样(称为verts的变量)。在对象中,我有一个setScalar()方法,该方法将采用此顶点矩阵并应用标量(这是一个简单的for循环乘以每个值)。当我创建一个新对象时,我应用标量,但当我创建一个新对象时,我不引用原始顶点数组,它似乎将标量应用于上一个对象中使用的缩放顶点 以下是verts阵列: var verts = [ // Front face -1.0, -1.0
var verts = [
// Front face
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
// Back face
-1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
// Top face
-1.0, 1.0, -1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
// Bottom face
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
// Right face
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
// Left face
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0
];
下面是我在对象中设置它的方式
//Cube object
function Cube(name, vertices, cubeVertInd, color, scale, positionX, positionY, positionZ)
{
this.name = name;
this.vertices = vertices;
this.cubeVertexIndices = cubeVertInd;
this.positionX = positionX;
this.positionY = positionY;
this.positionZ = positionZ;
this.setColor(color);
this.setScale(vertices, scale);
}
这是setScale方法
Cube.prototype.setScale = function(vertices, scale)
{
var length = vertices.length;
for( var i = 0; i < length; i++)
{
vertices[i] *= scale;
}
}
Cube.prototype.setScale=函数(顶点,比例)
{
变量长度=顶点长度;
对于(变量i=0;i
我知道是这样的,因为当我使用webgl时,我会根据第一个对象的大小得到不同的大小。如果我将对象设置为“缩放2”,然后将下一个对象设置为“缩放0.1”,即使原始顶点都是1,我得到0.2作为第二个对象的缩放。这表明使用的是我在第一个对象中设置的比例,而不是顶点。您必须克隆
顶点
数组,否则它始终是对原始顶点
数组的引用
试试这个:
//Cube object
function Cube(name, vertices, cubeVertInd, color, scale, positionX, positionY, positionZ)
{
// make a copy of the vertices
vertices = vertices.slice();
this.name = name;
this.vertices = vertices;
this.cubeVertexIndices = cubeVertInd;
this.positionX = positionX;
this.positionY = positionY;
this.positionZ = positionZ;
this.setColor(color);
this.setScale(vertices, scale);
}
如果仅为此使用
setScale()
,则可以使用Array.prototype.map()
方法并删除函数setScale()
在这种情况下,将创建另一个新对象,并将其缩放和指定给this.vertices
function Cube(name, vertices, cubeVertInd, color, scale, positionX, positionY, positionZ)
{
this.name = name;
this.vertices = vertices.map(function(value){
return value*scale;
});
this.cubeVertexIndices = cubeVertInd;
this.positionX = positionX;
this.positionY = positionY;
this.positionZ = positionZ;
this.setColor(color);
}
这是对
的回答,当我设置位置时,会发生类似的事情。我是否可以使这些位置对对象的每次迭代都是唯一的?
这个问题的一般解决方案是使用克隆函数,该函数可以返回对象的克隆(而不是引用)。因此,假设您有一个具有{verts:[1,2,3..],positionX:5,…}
等的基本对象,那么在创建新对象时要执行的操作是newObj=clone(baseObj),然后是newObj.setScale(-1)
以及所述克隆功能的一种实现:
var clone = function(obj,visited){
if (obj === null || typeof(obj) !== "object"){ // primitives and functions
return obj;
}
visited = visited || [];
if (visited.indexOf(obj)===-1){ // enable cloning objects with circular references at cost of linear search;
visited.push(obj);
if (obj instanceof RegExp){
return new RegExp(obj);
} else if (obj instanceof Date){
return new Date(obj);
} else if (Array.isArray(obj)){ // the [[Class]] of arrays can only be set by using Array constructor or [];
var newObj = [];
} else if (obj.constructor !== undefined){
newObj = Object.create(obj.constructor.prototype);
} else {
throw new TypeError("How to clone object: "+obj+" is undefined.", obj);
}
} else { // if object already seen/ circular reference; just return reference to it
return obj;
}
for(var key in obj) { // recursive call itself;
if(obj.hasOwnProperty(key)) {
newObj[key] = clone(obj[key],visited);
}
}
return newObj;
};
对不起,我还有一个问题。请参见
this.positionX
当我设置位置时,会发生类似的情况。我是否可以使这些位置对对象的每次迭代都是唯一的?