如何停止共享方法与类和扩展?

发布于 2025-02-13 12:24:23 字数 1145 浏览 1 评论 0原文

我正在创建两个类,矩形square,基于 mdn

由于矩形是正方形的原型,因此它们共享相同的方法区域()

但是,我希望afear()仅在length ===高度时才能运行。

我尝试在super()之后添加一个if-statement,但是IT区域()仍然返回this.length* this.height,无论错误输入如何。

我可以在Area()方法中添加if-statement,但我之所以没有,是因为当我们计算矩形的面积时,这是没有意义的。

class Rectangle {
  constructor(length, height){
    this.length = length;
    this.height = height;
  }
  area(){
    return this.length*this.height;
  }
}

class Square extends Rectangle {
  constructor(length, height){ // have area() return "Not a square" instead of a number
    super(length, height);
    if (length !== height) {
      console.log("Not a square.")
    }
    this.name = "square";
  }
}

const rectangle = new Rectangle(2,3);
console.log(rectangle.area());

const square = new Square(5,2);
console.log(square.area())

I am creating two classes, Rectangle and Square, based on the example shown on MDN.

Since Rectangle is the prototype of Square, they share the same method area().

However, I want area() to run only if length === height for the square.

I've tried to add an if-statement after super(), but it area() still returns this.length* this.height regardless of an erroneous input.

I could add an if-statement in the area() method, but I did not because it would not make sense when we are calculating the area of a rectangle.

class Rectangle {
  constructor(length, height){
    this.length = length;
    this.height = height;
  }
  area(){
    return this.length*this.height;
  }
}

class Square extends Rectangle {
  constructor(length, height){ // have area() return "Not a square" instead of a number
    super(length, height);
    if (length !== height) {
      console.log("Not a square.")
    }
    this.name = "square";
  }
}

const rectangle = new Rectangle(2,3);
console.log(rectangle.area());

const square = new Square(5,2);
console.log(square.area())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

维持三分热 2025-02-20 12:24:24

当条件不匹配时,您可以丢弃错误。这将停止对物体的构造。

class Square extends Rectangle {
  constructor(length, height){ 
    super(length, height);
    if (length !== height) {
      throw new Error("not a square");
    }
    this.name = "square";
  }
}

... 


try {
  const notASquare = new Square(10,20); // This will throw an error
} catch(e) {
  console.log(e)
}

但是,

在这种情况下,我认为仅接受1个参数以防止此错误的情况更加有效:

class Square extends Rectangle {
  constructor(size) {
    super(size, size); // Passing size 2 times makes sure length and height for a square are always equal
    this.name = "square";
  }
}

You can throw an error when the criteria is not matched. This will stop the construction of the object.

class Square extends Rectangle {
  constructor(length, height){ 
    super(length, height);
    if (length !== height) {
      throw new Error("not a square");
    }
    this.name = "square";
  }
}

... 


try {
  const notASquare = new Square(10,20); // This will throw an error
} catch(e) {
  console.log(e)
}

However

in this case i think it makes more sence to only accept 1 parameter to prevent this error from happening:

class Square extends Rectangle {
  constructor(size) {
    super(size, size); // Passing size 2 times makes sure length and height for a square are always equal
    this.name = "square";
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文