2013-03-06 25 views
0

考慮下面的代碼:如何在另一個類(ClassB)內實例化一個類(ClassA)並將ClassA對象用作JavaScript中ClassB的屬性?

function Coord(x, y) { 
    this.x = x; 
    this.y = y; 
} 

function Ellipse() { 
    this.Text = Text; 
    this.Cx = Cx; 
    this.Cy = Cy; 
    this.Rx = Rx; 
    this.Ry = Ry; 
} 

現在在功能Ellipse而不是使用CxCy等我想實例化功能Coord每對取得成就如下:

function Coord(x, y) { 
    this.x = x; 
    this.y = y; 
} 

function Ellipse() { 
    this.Text = Text; 
    Coord C = new C(); // where C has its own properties x and y 
    Coord R = new R(); // where R has its own properties x and y 
} 

回答

0

試試這個:

function Coord(x, y) { 
    this.x = x; 
    this.y = y; 
} 

function Ellipse(text, cx, cy, rx, ry) { 
    this.text = text; 
    var c = new Coord(cx, cy); 
    var r = new Coord(rx, ry); 
} 

我不知道你是怎麼想的o f Coord C = new C()但它是絕對錯誤的。 JavaScript變量沒有類型。

另外從哪裏得到Text, Cx, Cy等?它們不應該作爲參數傳遞給構造函數嗎?

相關問題