如何在另一个类(ClassB)中实例化一个类(ClassA),并在JavaScript中使用ClassA对象作为Clas

How to instantiate a class(ClassA) inside another class(ClassB) and use the ClassA object as a property in ClassB in JavaScript?

本文关键字:ClassA 并在 JavaScript 对象 Clas 另一个 ClassB 实例化 一个      更新时间:2023-09-26

考虑以下代码:

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
}

试试这个:

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);
}

我不知道你是怎么想到Coord C = new C()的,但这绝对是错误的。JavaScript变量没有类型

也从哪里得到Text, Cx, Cy等?它们不应该作为参数传递给构造函数吗?