Ninjadoodle Posted June 18, 2018 Share Posted June 18, 2018 Hi @enpu / panda people This is just a basic question about declaring variables and stuff. I'm just trying to learn to write more efficient code and use less space lol. game.createScene('Stage04', { slot1: false, slot2: false, slot3: false, slot4: false, init: function() { if (this.slot1 === false) { // do something } The code however works exactly the same, if I remove the variable declarations, like below ... game.createScene('Stage04', { init: function() { if (this.slot1 === false) { // do something } Is it better 'practice' to have the declarations there, or does it really serve no purpose in this case? Thanks heaps for any feedback Quote Link to comment Share on other sites More sharing options...
enpu Posted June 18, 2018 Share Posted June 18, 2018 Actually those two codes won't work exactly same. if (this.slot1 === false) { // do something } If you don't define your variable, it's value will be "undefined", so that if statement will be false. Personally i think it is more clear to define all the properties in the beginning of the class, especially in big classes. Look at the source code of Panda engine, every class has it's properties defined in the beginning of the class, and every single property and function is in alphabetical order, pretty clean code i would say Quote Link to comment Share on other sites More sharing options...
8bitdna Posted June 18, 2018 Share Posted June 18, 2018 In other languages I try and define anything upfront for clarity and also its useful if I want to comment things sometimes, doing the same in my baby steps in JS and Panda as well. 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.