Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.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
Javascript 类中的变量范围_Javascript_Class_Variables_Scope - Fatal编程技术网

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(…)