Pryme8 Posted January 24, 2018 Share Posted January 24, 2018 So if one can access mesh._enabled and can modify it directly what is the point of the setters and getters for it? Why not just strip the _ and make mesh.enabled the standard? so like mesh.enabled = <Boolean> vs <mesh>.setEnabled(<Boolean>); and mesh.enabled == ? vs <mesh>.isEnabled(); Seems redundant. Quote Link to comment Share on other sites More sharing options...
satguru Posted January 24, 2018 Share Posted January 24, 2018 you cannot access "mesh._enabled" if your are programming in TypeScript. its probably defined as "private" in TypeScript. unfortunately JavaScript does not have anything equivalent to make a property private. so the only thing you can do is follow some convention like underscore to make the JavaScript programmer aware they are accessing a private property and let them know if they do so BABYLON cannot guarantee proper behavior. Pryme8 1 Quote Link to comment Share on other sites More sharing options...
tips4design Posted January 24, 2018 Share Posted January 24, 2018 @satguru You can make properties private in JavaScript, eg: function MyClass() { this._mesh = 'test'; return { setMesh: (val) => this._mesh =val, getMesh: () => this._mesh } } var a = new MyClass(); console.log(a._mesh); // undefined console.log(a.getMesh()); // test a.setMesh('NEW'); console.log(a.getMesh()); // NEW Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted January 24, 2018 Author Share Posted January 24, 2018 Gotcha! Was wondering! thank you. Quote Link to comment Share on other sites More sharing options...
satguru Posted January 24, 2018 Share Posted January 24, 2018 @tips4design nice to know. but I suspect some overhead to this approach. a quick google search bring's up some interesting discussion https://stackoverflow.com/questions/12713659/typescript-private-members https://yakovfain.com/2015/06/30/the-private-in-typescript-is-kinda-private/ Quote Link to comment Share on other sites More sharing options...
tips4design Posted January 25, 2018 Share Posted January 25, 2018 @satguru Oh, so the issue is actually that the library is written in TypeScript but OP writes in JavaScript, so he doesn't get the TS warnings while coding and sees the normal, public _mesh variable from the compiled code. I actually did not know that TypeScript private is compiled to public members instead of locally enclosed private variables. Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted January 25, 2018 Share Posted January 25, 2018 @Pryme8: we keep the setEnabled() and isEnabled for two reasons: - Back compat - Support for inheritance (You cannot overload properties on child class) Pryme8 1 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.