Calsa Posted November 22, 2018 Share Posted November 22, 2018 I am having another noob "this" moment. I'm working on a simple class.... These are a few snips of the problem area. When I call a method such as nodeActions below which binds actions to a mesh, then I click on the mesh triggering moveToNode, the this scope changes to actionManager's this instead of this classes this. Why this happens makes sense to me, but what is the best way to access the classes this scope from within moveToNode so I don't have to do such a sloppy hack to re access scene and my class variables from within the moveToNode method? moveToNode(){ var scene = this._actionManager._scene //HACK var pickedNode = scene.pick(scene.pointerX, scene.pointerY, null, false, scene.camera) var moveTarget = scene.getMeshByName(pickedNode.name) // More to come this is not a simple interpolate function } nodeActions(mesh) { mesh.actionManager = new BABYLON.ActionManager(this.scene) mesh.actionManager.registerAction( new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnPickTrigger, this.moveToNode)) } Quote Link to comment Share on other sites More sharing options...
Sebavan Posted November 22, 2018 Share Posted November 22, 2018 So either you rely on => instead of pure callback: https://hackernoon.com/javascript-es6-arrow-functions-and-lexical-this-f2a3e2a5e8c4 Or you can bind moveToNode to the desired context this.moveToNode.bind(this); Quote Link to comment Share on other sites More sharing options...
Calsa Posted November 22, 2018 Author Share Posted November 22, 2018 bind method is working, thanks. Is either method above programmatically better (more efficient, better practice, ext...)? Quote Link to comment Share on other sites More sharing options...
Sebavan Posted November 22, 2018 Share Posted November 22, 2018 I do not know tbh as they end up doing the same. Now arrow functions are not supported everywhere (it is on all major browsers) so bind might have a bit of more cross plat advantage. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.