Jump to content

OOP Questions - Raised by Pixi


MonsieurPixel
 Share

Recommended Posts

Hi there,

 

Coming from an AS3 background, I still have a rather partial understanding of all the OOP concepts in Javascript, which I used for years as a third wheel language. Well, who's laughing now ?! Certainly not me :(

 

#1 - I've seen that the Pixi.js class was encapsulated in an anonymous function :

(function(){	//stuff...}).call(this);

Is that the same as ?

(function(){	//stuff...})();

And what is it for ? As far as I know, anonymous functions are dynamically executed at runtime, but so would be the code itself. Plus everything is declared on PIXI so you're not polluting the global namespace and are not benefiting in any way from the closure.

 

#2 - Another dull question : in my game, I'm setting up a GameClip class :

http://pixelshaped.com/pixigame/js/game/GameClip.js

After that, I call my Game class :

http://pixelshaped.com/pixigame/js/game/Game.js

 

Fact is, I refer in my GameClip class to static vars from the Game class (like Game.GRAVITY = .3;) and that are still undeclared at the moment the GameClip.js file is loaded. It works, but I expected that it wouldn't. Can you tell me why ? Is that just because the GameClip functions aren't called yet (because they are used only by the Game class) or is it because of hoisting (i.e. classes would be declared at the same time because of #1) ?

 

P.S. : This is not strictly related to Pixi so feel free to move this topic elsewhere (in general discussions maybe ?)

Link to comment
Share on other sites

#1 The difference between using (function() {})() and (function() {}).call(this) is that the context (`this`) will be the same 100% of the time outside and inside the second form. The first form will have `window` as the context in non strict-mode javascript, and undefined in strict-mode. The reason we do that is because there are some variables that are declared outside of PIXI itself, which would pollute the global namespace.

 

#2 All your references to Game static members are within function bodies, code that is not executed ntil the function is called. By the time the function is actually called, both scripts have been parsed and so those variables are defined.

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...