MonsieurPixel Posted October 21, 2013 Share Posted October 21, 2013 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.jsAfter 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 ?) Quote Link to comment Share on other sites More sharing options...
xerver Posted October 21, 2013 Share Posted October 21, 2013 #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. Quote Link to comment Share on other sites More sharing options...
MonsieurPixel Posted October 28, 2013 Author Share Posted October 28, 2013 #1 I was going to ask you to be more specific but, based on what you said, I browsed Stackoverflow and I think I get it. #2 It makes sense. Thanks ! 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.