1

are there any specific disadvantages of defining a global function, as there are for defining global variables? We are trying to downsize our custom JS file, and quite a bit of code is being used across functionalities.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • Related: [What does it mean global namespace would be polluted?](http://stackoverflow.com/questions/8862665/what-does-it-mean-global-namespace-would-be-polluted) – apsillers Oct 13 '14 at 14:11
  • 1
    Image everyone would do this, what's the chance for naming conflicts etc when you start including some 3rd party js plugins for example? – Jeroen1984 Oct 13 '14 at 14:12
  • if all you need is couple of functions then you don't need to care. – webduvet Oct 13 '14 at 14:29
  • 1
    Global functions are global variables (they just happen to have function objects as their values). The disadvantages are the same as for other variables. – Quentin Oct 13 '14 at 15:53

3 Answers3

0

Namespace collisions are one big issue. If you create a global function, and accidentally use the same name as another global function, you will overwrite that function, which may cause your application to break.

Bear in mind that all global variables are actually properties of the window object. If you accidentally overwrite a built-in property of window, you might produce some really odd glitches that can be hard to track down.

Another reason is just general cleanliness/organization. If you write a dozen functions to do operations on strings, you might want to put them all on an object stringOps, so that you've got them in one place.

Raphael Serota
  • 2,157
  • 10
  • 17
0

The main danger with global references are that you will step on someone elses reference or vice versa. This can make testing extremely difficult as you may have introduced a large dose of non-determinism. For instance say you define Global.PI as 3.14159 and your functions which ref that work fine, until a user loads a page with a library that defines Global.PI as 'Lemon', now your functions work not quite as expected.

The project I work on has global references due to the strucutre of our application. To alleviate some of these issues we will attach a single object to window (window.yourObjHere) and place our global references within that object.

The only advantage to Globals are that some things simply can't be done without them. We have an eventBus that must message over seperate AngularJS applications. As the AngularJS native eventBus is per application, we have to register each application on our GLobal object in order to keep track of where messages should go etc. Whenver possible avoid using Globals but there are simply some things which can't be done without them. Just be careful and limit your Global footprint.

mccainz
  • 3,478
  • 5
  • 35
  • 45
0

The disadvantages are as poeple ahve outlined (basically no namespacing meaning functions/variables can be overwritten) but you could get around this by declaring a global object which has all you need in it - and you can add things to it 'on the fly' :

GLOBAL_myObject = {
    variableOne: 1,
    variableTwo: "A atring",
    getThisAndThat: function(p1,p2) {
       //do some stuff
       return p1 * p2; /or somthing
    }

};

So refer to all your own variables as a property of that object:

alert(GLOBAL_myObject.variableOne);
GLOBAL_myObject.getThisAndThat(1,2);

Effectively you're just namespacing the variables.

to add more to the global GLOBAL_myObject you can just declare it ..

GLOBAL_myObject.anotherThing=1965; // what a year!

user2808054
  • 1,376
  • 1
  • 11
  • 19