[英]Angular 2 objects, properties and variable scope
I have a Angular 2 object with the following properties: 我有一个具有以下属性的Angular 2对象:
export class ThreedViewerComponent implements OnInit {
private aScene: THREE.Scene;
private ThreeModel: THREE.Mesh;
and the following method: 和以下方法:
private initStage() {
// create a WebGL scene, camera and raycaster. Raycasting is used for mouse picking (working out what objects in the
// 3d space the mouse is over) amongst other things.
this.aCamera = new THREE.PerspectiveCamera(45, this.aSceneSettings.width / this.aSceneSettings.height, 0.1, 2000);
this.aScene = new THREE.Scene();
this.aRaycaster = new THREE.Raycaster;
this.ThreeModel = new THREE.Mesh;
this.loader = new PLYLoader();
this.loader.load('assets/data/GuyFawkesMask.ply', function (geometry) {
// Create 3D model
this.ThreeModel = new THREE.Mesh(geometry,
new THREE.MeshPhongMaterial(
{
color: 0xFFFFFF,
//vertexColors: THREE.VertexColors,
shading: THREE.FlatShading,
shininess: 0
})
);
this.aScene.add(this.ThreeModel);
});
I think I am confused about variable scopes. 我认为我对可变范围感到困惑。
I can make the code working by getting rid of the references this.ThreeModel
and this.aScene
inside the this.loader.load
function. 我可以使代码通过摆脱引用的工作
this.ThreeModel
和this.aScene
内部this.loader.load
功能。
I thought that external variables (properties) are visible inside a function in JavaScript but it does not seem to be the case. 我认为外部变量(属性)在JavaScript的函数内部可见,但事实并非如此。
The reason why I want to use this.ThreeModel
and this.aScene
is that I can get old of the current model and scene and do some clean up when loading a new model. 为什么我要使用的原因
this.ThreeModel
和this.aScene
的是,我能得到老当前的模型和场景,并做一些清理加载一个新的模型时。
What's wrong in my assumptions? 我的假设有什么问题?
Try using ES6 arrow function: 尝试使用ES6箭头功能:
this.loader.load('assets/data/GuyFawkesMask.ply', (geometry) => {
// Create 3D model
this.ThreeModel = new THREE.Mesh(geometry,
new THREE.MeshPhongMaterial(
{
color: 0xFFFFFF,
shading: THREE.FlatShading,
shininess: 0
})
);
this.aScene.add(this.ThreeModel);
});
You can use static
and access it within your class block. 您可以使用
static
并在您的类块中访问它。
export class ThreedViewerComponent implements OnInit {
static aScene: THREE.Scene;
static ThreeModel: THREE.Mesh;
And in the function: 并在函数中:
private initStage() {
// create a WebGL scene, camera and raycaster. Raycasting is used for mouse picking (working out what objects in the
// 3d space the mouse is over) amongst other things.
this.aCamera = new THREE.PerspectiveCamera(45, this.aSceneSettings.width / this.aSceneSettings.height, 0.1, 2000);
ThreedViewerComponent.aScene = new THREE.Scene();
this.aRaycaster = new THREE.Raycaster;
ThreedViewerComponent.ThreeModel = new THREE.Mesh;
this.loader = new PLYLoader();
this.loader.load('assets/data/GuyFawkesMask.ply', function (geometry) {
// Create 3D model
ThreedViewerComponent.ThreeModel = new THREE.Mesh(geometry,
new THREE.MeshPhongMaterial(
{
color: 0xFFFFFF,
//vertexColors: THREE.VertexColors,
shading: THREE.FlatShading,
shininess: 0
})
);
ThreedViewerComponent.aScene.add(ThreedViewerComponent.ThreeModel);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.