Jump to content

Panda / Javascript question :)


Ninjadoodle
 Share

Recommended Posts

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 :)

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...