Javascript 类中的变量范围
我有以下代码:Javascript 类中的变量范围,javascript,class,variables,scope,Javascript,Class,Variables,Scope,我有以下代码: class Cube { constructor(x, y, z, w, h, d, color) { this.x = x; this.y = y; this.z = z; this.w = w; this.h = h; this.d = d; this.color = color; let cube = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), new TH
class Cube {
constructor(x, y, z, w, h, d, color) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
this.h = h;
this.d = d;
this.color = color;
let cube = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), new THREE.MeshPhongMaterial({color: color}));
}
add() {
scene.add(cube);
}
static render() {
renderer.render(scene, camera);
}
}
我正在尝试创建一个Three.js,但这不是重点,所以把render()方法放在一边,因为这不是我要关注的
我在
constructor()
中有cube
变量,但我希望能够访问add()
中的cube
变量,但由于作用域的原因,它会引发错误。有没有一种方法可以使它能够在整个类中使用,而不必将变量声明放在类多维数据集
之外?如果我不清楚,很抱歉。将其作为实例属性,与所有其他属性一样。在构造函数中:
this.cube = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), new THREE.MeshPhongMaterial({color: color}));
然后参考:
scene.add(this.cube);
与所有其他属性一样,将其作为实例属性。在构造函数中:
this.cube = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), new THREE.MeshPhongMaterial({color: color}));
然后参考:
scene.add(this.cube);
this.cube=new Three.Mesh(…)
this.cube=new Three.Mesh(…)代码>