0

How can I set and access a global var in a prototype way?

var app;

(function(){
    "use strict";
    var App = function() {

    };
    App = App;
}(window));

$(function() {                    
    app = new App();
});
Gwenc37
  • 2,064
  • 7
  • 18
  • 22
panthro
  • 22,779
  • 66
  • 183
  • 324
  • 3
    I don't think `App = App` is doing what you think it is. `window.App = App` is probably what you're after. – Matt Jul 10 '14 at 13:17
  • Thanks, but how would I set a global var? – panthro Jul 10 '14 at 13:19
  • 1
    By doing just that; `window.yourGlobalVar = theValueYouWantToBeAccessedGlobally`. – Matt Jul 10 '14 at 13:19
  • Which variable are trying to set/get access to? – Huangism Jul 10 '14 at 13:19
  • possible duplicate of [How to declare a global variable in a .js file](http://stackoverflow.com/questions/944273/how-to-declare-a-global-variable-in-a-js-file) – cookie monster Jul 10 '14 at 13:25
  • @cookiemonster: Not really willing to cast a duplicate on that, as none of the answers in that question handle the IIFE situation which ***this*** question evolves around. – Matt Jul 10 '14 at 13:29
  • 1
    @Matt: IIFE is irrelevant. It doesn't change the way to create a global from within a function. Main thing is that OP needs to search before asking. This question has been asked [many timnes](https://www.google.com/search?q=javascript+how+to+create+global++variable+site%3Astackoverflow.com&gws_rd=ssl), which is no surprise since creating a global variable is not uncommon. – cookie monster Jul 10 '14 at 13:30
  • It sure does in strict mode, usually just omitting the `var` keyword creates a global in another scope, here it wouldn't, but of course attaching it as a property to the window would always work, so it's sort of a duplicate as the most upvoted answer in that question would work here as well, but it's not really the same question ? – adeneo Jul 10 '14 at 13:34
  • @cookiemonster: It does. You can pass in the global object to the IIFE, and reference it using the parameter name, just a adeneo used in his answer. That technique isn't explained anywhere in your dupe, which is quite a crucial omission. I'm not saying there's not a dupe of this, I'm saying the question you linked to isn't it. – Matt Jul 10 '14 at 13:34
  • @Matt: You can do that in any function, or not pass it at all. Doesn't matter. The way to create a global doesn't change. He's asking how to create a global, and it has been answered in the dupe I posted. – cookie monster Jul 10 '14 at 13:35
  • @cookiemonster: ... and where is that behavior explained in the duplicate you proposed? It's **not** a dupe, but I'll reiterate what I said before; I'm not saying there's not a dupe of this, I'm saying the question you linked to isn't it. Hopefully other community members will see sense and not cast votes against that either. /o – Matt Jul 10 '14 at 13:36
  • What behavior? You can create a global by adding a property to the `window` object, as [this answer](http://stackoverflow.com/a/944373/3096782) shows. You're making little sense. – cookie monster Jul 10 '14 at 13:38
  • @adeneo: Creating globals by omitting `var` is just bad practice IMO. The property way is to add properties to the global object as your answer and some answers in other question shows. Given that, strict mode won't make a difference. I do think it's the same. At least I fail to see how it differs in any substantial way. – cookie monster Jul 10 '14 at 13:42

1 Answers1

2

When you're using strict mode, the value of this inside the IIFE isn't window, it's probably undefined, so App isn't really global.

If you explicitly make it global it should work

var app;

(function (w) {
    "use strict";

    w.App = function () {

    };

}(window));

$(function () {
    app = new App();
});

FIDDLE

If you weren't using strict mode, you could just remove the var keyword

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • To clean up, you either probably want to stop `window` being passed to the IIFE, or give a name to the parameter. – Matt Jul 10 '14 at 13:20
  • @Matt - doesn't really matter, but sure, lets use the argument ! – adeneo Jul 10 '14 at 13:22
  • I agree it didn't break anything, but it was likely confusing/ misleading for people new to the concept. – Matt Jul 10 '14 at 13:23