2012-12-10 39 views
2

该代码使用继承的孩子,我在场景中添加一些东西,在父声明我怎么能做到这一点得到它在看到现场错误的子级别我怎么能在子代码中使用父变量?

function parent (domElement, renderStatistics) { 
    this.scene = new THREE.Scene(); 
    } 
function child(domElement) { 
    parent.call(this, domElement); 
    this.init(); 
} 
child.prototype = Object.create(parent.prototype); 

child.prototype.constructor = Young; 


child.prototype.init = function() { 
function createLab(geometry) { 
     var mesh = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial()); 
     this.scene.add(mesh); // this error Cannot call method 'add' of undefined 
    } 
} 

回答

2
child.prototype.init = function() { 
var _this = this; 
    function createLab(geometry) { 
     var mesh = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial()); 
     _this.scene.add(mesh); 
    } 
} 
0

我不知道为什么你需要创建内部初始化内部函数...

尝试要么

child.prototype.init = function() { 
     var mesh = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial()); 
     this.scene.add(mesh); // this error Cannot call method 'add' of undefined 
} 

或者

function createLab(geometry) { 
    var mesh = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial()); 
    this.scene.add(mesh); // this error Cannot call method 'add' of undefined 
}; 

child.prototype.init = function() { 
    createLab.call(this, whatever); 
} 
1

看起来像你的错误的原因是在第二行的双等于= =

这导致值的归属是一个布尔值,而不是你所期望的新的THREE.Mesh的实例。

+1

感谢这是我的错误没有在代码中 –

相关问题