对象数组

    function Point(x, y) {
        this.x = x;
        this.y = y;
    }
    function displayPts(arr) {
        for ( var i = 0; i < arr.length; ++i) {
            document.write(arr[i].x + ", " + arr[i].y + "<br />");
        }
    }
    var p1 = new Point(1, 2);
    var p2 = new Point(3, 5);
    var p3 = new Point(2, 8);
    var p4 = new Point(4, 4);
    var points = [ p1, p2, p3, p4 ];
    for ( var i = 0; i < points.length; ++i) {
        document.write("Point " + parseInt(i + 1) + ": " + points[i].x + ", "
                + points[i].y + "<br />");
    }
    var p5 = new Point(12, -3);
    points.push(p5);
    document.write("After push: " + "<br />");
    displayPts(points);
    points.shift();
    document.write("After shift: " + "<br />");
    displayPts(points);
    /* 运行结果如下:
    Point 1: 1, 2
    Point 2: 3, 5
    Point 3: 2, 8
    Point 4: 4, 4
    After push: 
    1, 2
    3, 5
    2, 8
    4, 4
    12, -3
    After shift: 
    3, 5
    2, 8
    4, 4
    12, -3 */

 

posted @ 2016-04-09 10:14  绯乐  阅读(153)  评论(0编辑  收藏  举报