Constructor 在三个JS中使用类构造函数
我想知道在三个js中使用JavaScript类是否也是常见的做法 我搜索了一些关于这方面的意见,大多数人说“使用原型”更常见 因为我比较熟悉类构造函数而不是原型,所以我试图找到一些使用类而不是原型的示例。(尽管原型和类所做的工作几乎相同) 例如: 如果我使用类构造函数而不是原型,会有问题或限制吗? 我尝试的方法如下Constructor 在三个JS中使用类构造函数,constructor,three.js,prototype,Constructor,Three.js,Prototype,我想知道在三个js中使用JavaScript类是否也是常见的做法 我搜索了一些关于这方面的意见,大多数人说“使用原型”更常见 因为我比较熟悉类构造函数而不是原型,所以我试图找到一些使用类而不是原型的示例。(尽管原型和类所做的工作几乎相同) 例如: 如果我使用类构造函数而不是原型,会有问题或限制吗? 我尝试的方法如下 class ParticleMesh { constructor(num, vels, type) { this.particleNum = num;
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();
}
}
class ParticleMesh{
构造函数(数量、级别、类型){
this.particleNum=num;
此.TimerStartFailding=10;
this.mesh=getPointMesh(num、vels、type);
}
更新(重力){
如果(this.timerstartlaving>0)this.timerstartlaving-=0.3;
const{position,velocity,color,mass}=this.mesh.geometry.attributes;
常量递减=()=>(Math.random()>0.5?0.98:0.96);
const decrementByVel=v=>(Math.random()>0.5?0:(1-v)*0.1);
for(设i=0;i 如果(this.timerstart)在Three.js之上选择的代码样式无关紧要,那么您仍然在使用Three的API。