Jump to content

JavaScript question, and phaser question


Will
 Share

Recommended Posts

Hello again

 

I'm new to JS in general, but I understand some of it (programmed a lot before).

I find I learn best by doing, hence making a game with phaser!

 

In all the examples, the code in inside this - 

(function () {    //Some code})();

What is the significance of the function? Works without it! 

 

Oh yes, and if I wanted to have multiple files (player.js, enemy.js ETC) how would I go about doing this?

 

Also, how would I change the background colour of the game?

Link to comment
Share on other sites

That is what is referred to as an immediate function.

You start with a function,

Function(){

}

Then immediately wrap it in parenthesis to make it an expression, like you might a math operation 2*(1+3)

(Function(){

})

Then finally you call it like you always call a function, using () so that everything inside of it is evaluated right away.

(Function(){

})()

---

It has several benefits, ill like a couple of key ones. It prevents any variables declared within it from leaking into the global scope (Window). It also allows you to create local variables in them that are private to the objects you create inside that function.

Scoping is a big pain in JavaScript, something they definitely got wrong - so we have to do things like this to help side step some if the common problems

Link to comment
Share on other sites

That is what is referred to as an immediate function.

You start with a function,

Function(){

}

Then immediately wrap it in parenthesis to make it an expression, like you might a math operation 2*(1+3)

(Function(){

})

Then finally you call it like you always call a function, using () so that everything inside of it is evaluated right away.

(Function(){

})()

---

It has several benefits, ill like a couple of key ones. It prevents any variables declared within it from leaking into the global scope (Window). It also allows you to create local variables in them that are private to the objects you create inside that function.

Scoping is a big pain in JavaScript, something they definitely got wrong - so we have to do things like this to help side step some if the common problems

I see! Thanks!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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