0

i've found this example code

    var global = (function () {
      return this || (1, eval)('this');
    }());

but i havn't understood its utility. When/why i should use this code?

From: http://shichuan.github.io/javascript-patterns/?utm_content=buffer874b9&utm_source=buffer&utm_medium=facebook&utm_campaign=Buffer

Alberto Fecchi
  • 1,705
  • 12
  • 27
  • 1
    You normally don't need this. As simple as that. – Denys Séguret Apr 18 '14 at 13:47
  • 3
    Read the description: `Description: access the global object without hard-coding the identifier window`. Usually (in a browser) the global object is `window`. In some environments (e.g. node JS) it isn't. As @dystroy said, you usually wouldn't need to worry about this. – Matt Burland Apr 18 '14 at 13:48
  • In non strict code, there are simpler ways to get the global object. For example `(function(){return this}).call()`. – Denys Séguret Apr 18 '14 at 13:49
  • 1
    @MattBurland Even in a library running both in a browser and in node, a properly designed program has rarely reasons to access the global object (I don't think you say otherwise, just trying to be clearer). – Denys Séguret Apr 18 '14 at 13:51
  • 1
    @dystroy: Absolutely agree. If you need the global object, your first thought should be to rethink what you are doing and come up with a better way. – Matt Burland Apr 18 '14 at 13:55
  • @Bergi This doesn't look like the good closing reason as OP already knows how to access the global object. – Denys Séguret May 06 '14 at 07:35

0 Answers0