constructor - 在三个 JS 中使用类构造函数
问题描述
我想知道使用 JavaScript 类 inn 三个 js 是否是常见的做法。
我搜索了一些关于这个的意见,大多数人说“使用原型”更常见。
由于我对原型的类构造函数相对更舒服,因此我尝试找到一些使用类而不是原型的示例。(即使原型和类在做几乎相同的工作)。
示例:https ://codepen.io/tksiiii/pen/VOEKLQ
如果我使用类构造函数而不是原型,会有问题或限制吗?我试图做的方法如下。
class ParticleMesh {
constructor(num, vels, type) {
this.particleNum = num;
this.timerStartFading = 10;
this.mesh = getPointMesh(num, vels, type);
}
update(gravity) {
if (this.timerStartFading > 0) this.timerStartFading -= 0.3;
const { position, velocity, color, mass } = this.mesh.geometry.attributes;
const decrementRandom = () => (Math.random() > 0.5 ? 0.98 : 0.96);
const decrementByVel = v => (Math.random() > 0.5 ? 0 : (1 - v) * 0.1);
for (let i = 0; i < this.particleNum; i++) {
const { x, y, z } = getOffsetXYZ(i);
velocity.array[y] += gravity.y - mass.array[i];
velocity.array[x] *= friction;
velocity.array[z] *= friction;
velocity.array[y] *= friction;
position.array[x] += velocity.array[x];
position.array[y] += velocity.array[y];
position.array[z] += velocity.array[z];
const { a } = getOffsetRGBA(i);
if (this.timerStartFading <= 0) {
color.array[a] *= decrementRandom() - decrementByVel(color.array[a]);
if (color.array[a] < 0.001) color.array[a] = 0;
}
}
position.needsUpdate = true;
velocity.needsUpdate = true;
color.needsUpdate = true;
}
disposeAll() {
this.mesh.geometry.dispose();
this.mesh.material.dispose();
}
}
谢谢你。
解决方案
推荐阅读
- arrays - 如何在 NEO4 Cypher 中创建属性数组?
- javascript - 地理定位已在本文档中被功能政策禁用
- python - 为什么在计算数组的子集时,变量名似乎有所不同?
- javascript - 如何使用 Material UI React 显示菜单列表中的哪个项目被选中?
- emacs - Emacs 奇怪的行移动
- ruby-on-rails - 在 Rails 5 中使用不同的 redis 存储进行会话和缓存
- visualization - 如何可视化纵向数据?
- mysql - 无法在 Linux 上设置对 MySQL 服务器的远程访问
- python - 为什么当我复制网络权重时它在 Pytorch 中,它会在反向传播后自动更新?
- php - 如何从 URL 中删除 id