簡體   English   中英

如何從JavaScript中的對象中的對象調用方法?

[英]How to call a method from an object in an object in javascript?

我是剛接觸javascript的人,我想知道是否有人可以幫助我如何從對象中的對象正確調用方法。 制作圓形花園似乎可行,我什至可以從那個花園叫一個圓形。 但是每當我想打電話給x時,它都會停止工作...

這是代碼:

var cg = new circlegarden(4);
var testx = cg.getCircle(0).getx();

function circle(x, y, vx, vy, dv, color) {
    this.x=x;
    this.y=y;
    this.vx=vx;
    this.vy=vy;
    this.dv=dv;
    this.color=color;
    this.vertices = [0.0, 0.0, 0.0];
    for(var i = 0; i <= 40; i++) {
        this.vertices.push(Math.cos((2 * Math.PI)/40)); 
        this.vertices.push(Math.sin((2 * Math.PI)/40)); 
        this.vertices.push(0); 
    }
    this.getx = function() {
        return this.x;
    }
    //other methods...
}

function circlegarden(n) {
    this.n=n;
    this.circles=new Array();
    for(var i = 0; i < this.n; i++) {
        this.circles.push(2*Math.cos((2 * Math.PI)/this.n), 2*Math.sin((2 * Math.PI)/this.n), 2*(Math.random()-.5), 2*(Math.random()-.5), 0.02, i);
    }
    this.wall=[[-2.9, -2.5, 2.9, -2.5], [-2.9, 2.5, 2.9, 2.5], [-2.9, -2.5, -2.9, 3], [2.9, -2.5, 2.9, 2.5]];

    this.getCircle = function(i) {
        return this.circles[i];
    }
    //other methods...
}

您沒有在該for循環中創建圈子。 您應該使用new circle(2*Math.cos((2 * Math.PI)/this.n), 2*Math.sin((2 * Math.PI)/this.n), 2*(Math.random()-.5), 2*(Math.random()-.5), 0.02, i)創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM