Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Constructor 在三个JS中使用类构造函数_Constructor_Three.js_Prototype - Fatal编程技术网

Constructor 在三个JS中使用类构造函数

Constructor 在三个JS中使用类构造函数,constructor,three.js,prototype,Constructor,Three.js,Prototype,我想知道在三个js中使用JavaScript类是否也是常见的做法 我搜索了一些关于这方面的意见,大多数人说“使用原型”更常见 因为我比较熟悉类构造函数而不是原型,所以我试图找到一些使用类而不是原型的示例。(尽管原型和类所做的工作几乎相同) 例如: 如果我使用类构造函数而不是原型,会有问题或限制吗? 我尝试的方法如下 class ParticleMesh { constructor(num, vels, type) { this.particleNum = num;

我想知道在三个js中使用JavaScript类是否也是常见的做法

我搜索了一些关于这方面的意见,大多数人说“使用原型”更常见

因为我比较熟悉类构造函数而不是原型,所以我试图找到一些使用类而不是原型的示例。(尽管原型和类所做的工作几乎相同)

例如:

如果我使用类构造函数而不是原型,会有问题或限制吗? 我尝试的方法如下

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。