0

我遇到了一些 javascripting 的问题,其中一些显然是数字的值突然变成了对象,我不知道如何或为什么。

代码片段:

addFigure("-1,1,-0.5_1,1,-0.5_0.5,-1,-0.5_-0.5,-1,-0.5");

function addFigure(t) {
        var fig = new figure();
        var v = t.split("_");

        var points = new Array();
        for (var i = 0; i < v.length; i++) {
            var coords = v[i].split(",");
            var x = parseFloat(coords[0]);
            var y = parseFloat(coords[1]);
            var z = parseFloat(coords[2]);
            alert(typeof x + " " +typeof y)
            var point = new Point3D(x, y, z);
            alert(typeof point.x + " " + typeof point.y)
           //both alerts print out "number number"
           fig.addPoint(point);
        }


        figures.push(fig);
    }

        function figure() {
        this.points = new Array();
        this.addPoint = function (x, y, z) {
            var v = new Point3D(x, y, z);
            alert(typeof x + " " + typeof y)
//this alert prints out "Object undefined"
            this.points.push(v)
        }

        this.getPoints = function () { return this.points }

    }
4

2 回答 2

2

您在这里addPoint使用一个参数 (a Point3D) 调用您的函数:

fig.addPoint(point);

addPoint似乎期望这一点作为三个单独的参数:

this.addPoint = function (x, y, z) {

所以你最终会x成为Point3D你传入的那个,y并且z成为undefined.

于 2012-08-06T13:04:55.553 回答
2

您的addPoint()方法似乎期望x,yz属性分别传递,但您只传递了一个参数,即point对象。

要么将方法更改为:

this.addPoint(point) {
    /* x, y and z are now retrievable from point.x, point.y etc */
}

或者将调用更改为方法

fig.addPoint(point.x, point.y, point.z);
于 2012-08-06T13:06:45.223 回答