我正在尝试使用if语句设置面向对象的javascript变量 如果用户输入字符串颜色,它将使用适当的数组 我设置顶点数组,但颜色数组给我带来麻烦 我一直在警报框上“未定义”。 这是我第一次尝试这个,我认为我使用这个。错误。
<script type="text/javascript">
function Cube( vertexPoints, Color )
{
this.vertices = vertexPoints;
if(Color == "yellow" || Color == "Yellow")
{
this.colorArray = [
[1.0, 1.0, 0.0, 1.0],
[1.0, 1.0, 0.0, 1.0],
[1.0, 1.0, 0.0, 1.0],
[1.0, 1.0, 0.0, 1.0],
[1.0, 1.0, 0.0, 1.0],
[1.0, 1.0, 0.0, 1.0]
];
}
else if(Color == "red" || Color == "Red")
{
this.colorArray = [
[1.0, 0.0, 0.0, 1.0],
[1.0, 0.0, 0.0, 1.0],
[1.0, 0.0, 0.0, 1.0],
[1.0, 0.0, 0.0, 1.0],
[1.0, 0.0, 0.0, 1.0],
[1.0, 0.0, 0.0, 1.0]
];
}
else if(Color == "green" || Color == "Green")
{
this.colorArray = [
[0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 1.0]
];
}
else if(Color == "pink" || Color == "Pink")
{
this.colorArray = [
[1.0, 0.5, 0.5, 1.0],
[1.0, 0.5, 0.5, 1.0],
[1.0, 0.5, 0.5, 1.0],
[1.0, 0.5, 0.5, 1.0],
[1.0, 0.5, 0.5, 1.0],
[1.0, 0.5, 0.5, 1.0]
];
}
else if(Color == "purple" || Color == "Purple")
{
this.colorArray = [
[1.0, 0.0, 1.0, 1.0],
[1.0, 0.0, 1.0, 1.0],
[1.0, 0.0, 1.0, 1.0],
[1.0, 0.0, 1.0, 1.0],
[1.0, 0.0, 1.0, 1.0],
[1.0, 0.0, 1.0, 1.0]
];
}
else if(Color == "blue" || Color == "Blue")
{
this.colorArray = [
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0]
];
}
else
{
this.colorArray = [
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0]
]
}
}
function hi()
{
alert("Hi my vertices is "+Cube.vertices);
alert("Hi my vertices is "+Cube.colorArray);
}
function set()
{
Cube.vertices = [
// 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.Color = "yellow";
}
</script>
<body>
<button type="button" onclick="set();">Set the properties</button>
<button type="button" onclick="hi();">Push me</button>
</body>
</head>
</html>
非常感谢任何帮助。
答案 0 :(得分:1)
function hi()
{
alert("Hi my vertices is "+Cube.vertices);
alert("Hi my vertices is "+Cube.colorArray);
}
什么是Cube
,不是类名,而是对象的实例?
set()
功能也是如此。
如果您想访问您所在的多维数据集的实际实例,请将其存储在另一个变量中,以便以后使用它:
var self = this;
function hi()
{
alert("Hi my vertices is "+self.vertices);
alert("Hi my vertices is "+self.colorArray);
}
答案 1 :(得分:1)
您的代码存在一些问题,您可以进行一些改进。
您的Cube对象表示可以将其视为其他语言的类。你的班级有一些属性。您需要创建一个辅助(setter)方法来设置所需的颜色矩阵。此外,您可以使用switch
:
function Cube(vertices, color) {
this.vertices = vertices;
this.setColor(color);
}
Cube.prototype.setColor = function(color) {
switch (color.toLowerCase()) {
case "yellow":
this.colorArray = [];
break;
case "red":
this.colorArray = [];
break;
case "purple":
this.colorArray = [];
break;
[...]
}
}
// create a new instance of your Cube class
var cubeInstance = new Cube([0, 1, 0 ...], "yellow");
// then change the color to red
cubeInstance.setColor("red");
答案 2 :(得分:0)
有几种方法可行。一个可以如下:
<script type="text/javascript">
function Cube( vertexPoints, Color )
{
this.vertices = vertexPoints;
if(Color == "yellow" || Color == "Yellow")
{
this.colorArray = [
[1.0, 1.0, 0.0, 1.0],
[1.0, 1.0, 0.0, 1.0],
[1.0, 1.0, 0.0, 1.0],
[1.0, 1.0, 0.0, 1.0],
[1.0, 1.0, 0.0, 1.0],
[1.0, 1.0, 0.0, 1.0]
];
}
else if(Color == "red" || Color == "Red")
{
this.colorArray = [
[1.0, 0.0, 0.0, 1.0],
[1.0, 0.0, 0.0, 1.0],
[1.0, 0.0, 0.0, 1.0],
[1.0, 0.0, 0.0, 1.0],
[1.0, 0.0, 0.0, 1.0],
[1.0, 0.0, 0.0, 1.0]
];
}
else if(Color == "green" || Color == "Green")
{
this.colorArray = [
[0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 1.0]
];
}
else if(Color == "pink" || Color == "Pink")
{
this.colorArray = [
[1.0, 0.5, 0.5, 1.0],
[1.0, 0.5, 0.5, 1.0],
[1.0, 0.5, 0.5, 1.0],
[1.0, 0.5, 0.5, 1.0],
[1.0, 0.5, 0.5, 1.0],
[1.0, 0.5, 0.5, 1.0]
];
}
else if(Color == "purple" || Color == "Purple")
{
this.colorArray = [
[1.0, 0.0, 1.0, 1.0],
[1.0, 0.0, 1.0, 1.0],
[1.0, 0.0, 1.0, 1.0],
[1.0, 0.0, 1.0, 1.0],
[1.0, 0.0, 1.0, 1.0],
[1.0, 0.0, 1.0, 1.0]
];
}
else if(Color == "blue" || Color == "Blue")
{
this.colorArray = [
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 1.0, 1.0]
];
}
else
{
this.colorArray = [
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0]
]
}
}
var cube = new Cube(); // can use parameters here, define object cube
function hi(cube)
{
alert("Hi my vertices is "+cube.vertices);
alert("Hi my vertices is "+cube.colorArray);
}
function set(cube)
{
cube.vertices = [
// 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.Color = "yellow";
}
</script>
<body>
<button type="button" onclick="set(cube);">Set the properties</button>
<button type="button" onclick="hi(cube);">Push me</button>
</body>
</head>
</html>
其他可以是:
Cube.prototype.set = function (v, c) { this.vertices = v; this.color = c } //call cube.set(v,c);