我试图在JS中理解OOP,但它对我来说并不适合...... 我尝试过的是以下内容:
getBalanceOfFamily
我得到第一个输出:var worldClass = function(shape){
this.shape = shape;
switch (shape){
case "sphere":
var tmpX = new THREE.Vector3(0, 0, 0);
var sphereModel = new THREE.Sphere( tmpX, 6);
var sphereGeom = new THREE.SphereGeometry( 6, 32, 16 );
var boundMaterial = new THREE.MeshBasicMaterial( { color: 0x0000ff, transparent: true, opacity: 0.3 } );
var boundSphere = new THREE.Mesh( sphereGeom, boundMaterial );
boundSphere.position.set(tmpX);
break;
default:
var tmpX = new THREE.Vector3(0, 0, 0);
var sphereModel = new THREE.Sphere( tmpX, 6);
var sphereGeom = new THREE.SphereGeometry( 6, 32, 16 );
var boundMaterial = new THREE.MeshBasicMaterial( { color: 0x0000ff, transparent: true, opacity: 0.3 } );
var boundSphere = new THREE.Mesh( sphereGeom, boundMaterial );
boundSphere.position.set(tmpX);
break;
}
this.home = boundSphere;
this.draw = function(){
scene.add(this.home);
}
this.setPosition = function(newPos){
this.home.position.set(newPos);
}
}
var world1 = new worldClass(new THREE.Vector3(0, 0, 0));
console.log(world1.home.position);
world1.setPosition(new THREE.Vector3(1, 1, 1));
console.log(world1.home.position);
,当我尝试更改位置时,我得到未定义的字段而不是undefined
。
我知道我可以为此目的使用财产,但我需要知道我的班级有什么问题
我还成功地通过添加一个"构造函数"来欺骗JS。函数到类,它包含了明显有问题的行:x: undefined, y:undefined, z:undefined
。
但是仍然 - 我希望我能理解在JS中处理类的正确方法是什么。
答案 0 :(得分:0)
你正在传递setPosition一个Vector3,它将position.x设置为Vector3 ...
我想我已经在这里解决了 http://codepen.io/snovak/pen/OMMoNZ
this.setPosition = function(newPos){
if(newPos.hasOwnProperty("x") && newPos.hasOwnProperty("y") && newPos.hasOwnProperty("z") ){
this.home.position.set(newPos.x, newPos.y, newPos.z ) ;
} else {
console.log("setPosition requires THREE.Vector3 as arguement");
}
}