-2

I read that it is usually best to include your javascript code within a function block like so

(function () {
    "use strict";
    // stuff here
}());

But if the code is meant to be globally accessible, a function or a constant for example, is that ok to be outside of a function block or should I set it up in another way?

Also - if I moved the code outside of the function block JSLint, for example, would suggest I move the use strict statement inside a function block. Would just have to be a concession I would have to make?

Community
  • 1
  • 1
Chris
  • 26,744
  • 48
  • 193
  • 345
  • Why downvote without a comment? How does that help anyone? – Chris Jan 14 '15 at 09:46
  • 1
    It seems like you're using a pattern without really understanding *why*: a good example of cargo cultism. You should dig deeper and find alternatives to global functions/variables. (This isn't related to downvoting.) – JJJ Jan 14 '15 at 09:48
  • @t.niese That looks great thanks. Juhana I know why I'm using it, I just didn't how to scope my code appropriately whilst having global functions. I thought the point of SO was that you could ask questions you don't know the answer to? – Chris Jan 14 '15 at 09:50
  • 2
    @Chris sorry was about to fix a typo, and accidentally hit delete. [How to declare global variables when using the strict mode pragma](https://stackoverflow.com/questions/9397778) – t.niese Jan 14 '15 at 09:50
  • @Juhana I have a file that provides helper functionality to an Angular ViewModel. Arguably it could be written as a directive but I didn't write the code, another developer did, and I don't have time to completely rewrite it & test it. I was just updating it be inside a function block (as I had related MVC bundling issues) when I realised I didn't know the best practice in this situation – Chris Jan 14 '15 at 09:54
  • @Chris Well downvotes are opinion based. It is not wrong to ask questions, but e.g. if passing `use strict global variables` to a search engine shows as first result the one I linked to, it is likely that you will get a down vote, as you neither linked to that question nor explain why your question differs from that one. But as I said I didn't downvote. – t.niese Jan 14 '15 at 09:55

1 Answers1

0

Setting things up inside of a function has benefits, it means that private variables which are not exposed remain private. You can expose things to the outside world by returning things from that function.

var module = (function() {
    "use strict";
    var private = "This is private!";
    var publicFn = function() { return "This is public!"; };

    return {
        myFunction: publicFn;
    };
})();

module.myFunction(); // This is public

As for using "use strict" only inside functions, it's because authors often concatenate and minify multiple JavaScript files, and using "use strict" outside the function scope will affect all other files in the chain (which is sometimes not the intended behavior and can cause bugs).

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308