0

明らかに数字であるいくつかの値が突然オブジェクトに変わるJavaScriptスクリプトに問題があり、その方法や理由がわかりません。

コードスニペット:

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()メソッドは、プロパティとプロパティが別々に渡されることを期待しているようxですyzpointオブジェクトという1つのパラメーターだけを渡しています。

メソッドを次のように変更します。

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 に答える
2

addPointここでは、1 つのパラメーター (a Point3D)を使用して関数を呼び出しています。

fig.addPoint(point);

しかしaddPoint、ポイントは3つの個別のパラメーターとして期待されているようです:

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

したがって、あなたはxPoint3Dあなたが渡されたものであり、yそしてでzあるundefined.

于 2012-08-06T13:04:55.553 に答える