javascript - JavaScript: Is it possible to add arguments to a class extension?
问题描述
I am using Toxiclibs Verlet physics engine in JavaScript, and I would like to extend the VerletParticle2D
class to create a custom object. It seems easy enough to add parameters and methods, but I wonder if my new class can have additional arguments. For example, VerletParticle2D
takes one argument, which is a position vector.
Could I simply call the the new class with an additional argument, and pass it two vectors, and then define how the new argument is used in the added methods?
解决方案
你当然可以像这样扩展一个类:
class BaseClass {
constructor(vector1) {
this.vector1 = vector1;
}
}
class MyClass extends BaseClass {
constructor(vector1, vector2) {
super(vector1);
this.vector2 = vector2;
}
}
然后用 . 创建一个新实例new MyClass(vector1, vector2)
。
推荐阅读
- sql - 选择带有浇头 SQL 的比萨饼
- python - 'await' 方法调用上的 SyntaxError
- php - 使用 symfony formbuider 上的表单生成器生成有关 NM 关系的单选按钮
- javascript - 如何允许用户在本机反应中输入文件?
- android - 如果我使用 BroadcastReceiver,Android CallScreeningService 放在哪里?
- ios - 无法在 Objective C 中分配子 VC 属性
- telerik-reporting - Telerik Report 参数中的验证
- junit - 当我使用@ExtendWith(MockitoExtension.class) 时如何修复错误?
- c - 打印 int_fast_32_t 等数据类型的可移植和正确方法是什么
- reactjs - useContext 没有更新某些组件,而它对其他人工作正常