Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

myLib.util.someUtilityFunction0; was repeated on 2 lines, causing confusion. If sticking to examples that append 0 and 1 to the end of function names, it seems strange to use '2'
Source Link

There is no magic with namespace objects, nor will you necessarily have any issues if you use lots of global variables. The main reason to use "namespace" objects is to reduce the potential for duplicate global variable names. A second reason is to group similar functions together for convenience, e.g:

// Object example (suggested best practice):
// DOM functions are under myLib.dom
myLib.dom.someDOMFunction0;
myLib.dom.someDOMFunction2;someDOMFunction1;

// Utility functions are under myLib.util
myLib.util.someUtilityFunction0;
myLib.util.someUtilityFunction0;someUtilityFunction1;

Note that the above has practically the same chance of duplicates as similarly global variables:

// Global variable example:
myLib_dom_someDOMFunction0;
myLib_dom_someDOMFunction1;
myLib_util_someUtilityFunction0;
myLib_util_someUtilityFunction1;

Of course the former is generally preferred because it seen as easier to work with. I'm not advocating that you adopt the second approach (I use the first), just pointing out that while there is an issue with creating lots of global variables, so–called "global namespace pollution" is greatly overrated as a hazard.

There is no magic with namespace objects, nor will you necessarily have any issues if you use lots of global variables. The main reason to use "namespace" objects is to reduce the potential for duplicate global variable names. A second reason is to group similar functions together for convenience, e.g:

// DOM functions are under myLib.dom
myLib.dom.someDOMFunction0;
myLib.dom.someDOMFunction2;
// Utility functions are under myLib.util
myLib.util.someUtilityFunction0;
myLib.util.someUtilityFunction0;

Note that the above has practically the same chance of duplicates as similarly global variables:

myLib_dom_someDOMFunction0;
myLib_dom_someDOMFunction1;
myLib_util_someUtilityFunction0;
myLib_util_someUtilityFunction1;

Of course the former is generally preferred because it seen as easier to work with. I'm not advocating that you adopt the second approach (I use the first), just pointing out that while there is an issue with creating lots of global variables, so–called "global namespace pollution" is greatly overrated as a hazard.

There is no magic with namespace objects, nor will you necessarily have any issues if you use lots of global variables. The main reason to use "namespace" objects is to reduce the potential for duplicate global variable names. A second reason is to group similar functions together for convenience, e.g:

// Object example (suggested best practice):
// DOM functions are under myLib.dom
myLib.dom.someDOMFunction0;
myLib.dom.someDOMFunction1;

// Utility functions are under myLib.util
myLib.util.someUtilityFunction0;
myLib.util.someUtilityFunction1;

Note that the above has practically the same chance of duplicates as similarly global variables:

// Global variable example:
myLib_dom_someDOMFunction0;
myLib_dom_someDOMFunction1;
myLib_util_someUtilityFunction0;
myLib_util_someUtilityFunction1;

Of course the former is generally preferred because it seen as easier to work with. I'm not advocating that you adopt the second approach (I use the first), just pointing out that while there is an issue with creating lots of global variables, so–called "global namespace pollution" is greatly overrated as a hazard.

Not "exactly" because dots are not allowed in identifiers, thus underscores are a form of in-band signaling
Source Link
André Chalella
  • 14.2k
  • 10
  • 59
  • 64

There is no magic with namespace objects, nor will you necessarily have any issues if you use lots of global variables. The main reason to use "namespace" objects is to reduce the potential for duplicate global variable names. A second reason is to group similar functions together for convenience, e.g:

// DOM functions are under myLib.dom
myLib.dom.someDOMFunction0;
myLib.dom.someDOMFunction2;
// Utility functions are under myLib.util
myLib.util.someUtilityFunction0;
myLib.util.someUtilityFunction0;

Note that the above has exactlypractically the same chance of duplicates as similarly global variables:

myLib_dom_someDOMFunction0;
myLib_dom_someDOMFunction1;
myLib_util_someUtilityFunction0;
myLib_util_someUtilityFunction1;

Of course the former is generally preferred because it seen as easier to work with. I'm not advocating that you adopt the second approach (I use the first), just pointing out that while there is an issue with creating lots of global variables, so–called "global namespace pollution" is greatly overrated as a hazard.

There is no magic with namespace objects, nor will you necessarily have any issues if you use lots of global variables. The main reason to use "namespace" objects is to reduce the potential for duplicate global variable names. A second reason is to group similar functions together for convenience, e.g:

// DOM functions are under myLib.dom
myLib.dom.someDOMFunction0;
myLib.dom.someDOMFunction2;
// Utility functions are under myLib.util
myLib.util.someUtilityFunction0;
myLib.util.someUtilityFunction0;

Note that the above has exactly the same chance of duplicates as similarly global variables:

myLib_dom_someDOMFunction0;
myLib_dom_someDOMFunction1;
myLib_util_someUtilityFunction0;
myLib_util_someUtilityFunction1;

Of course the former is generally preferred because it seen as easier to work with. I'm not advocating that you adopt the second approach (I use the first), just pointing out that while there is an issue with creating lots of global variables, so–called "global namespace pollution" is greatly overrated as a hazard.

There is no magic with namespace objects, nor will you necessarily have any issues if you use lots of global variables. The main reason to use "namespace" objects is to reduce the potential for duplicate global variable names. A second reason is to group similar functions together for convenience, e.g:

// DOM functions are under myLib.dom
myLib.dom.someDOMFunction0;
myLib.dom.someDOMFunction2;
// Utility functions are under myLib.util
myLib.util.someUtilityFunction0;
myLib.util.someUtilityFunction0;

Note that the above has practically the same chance of duplicates as similarly global variables:

myLib_dom_someDOMFunction0;
myLib_dom_someDOMFunction1;
myLib_util_someUtilityFunction0;
myLib_util_someUtilityFunction1;

Of course the former is generally preferred because it seen as easier to work with. I'm not advocating that you adopt the second approach (I use the first), just pointing out that while there is an issue with creating lots of global variables, so–called "global namespace pollution" is greatly overrated as a hazard.

Source Link
RobG
  • 148.1k
  • 32
  • 180
  • 216

There is no magic with namespace objects, nor will you necessarily have any issues if you use lots of global variables. The main reason to use "namespace" objects is to reduce the potential for duplicate global variable names. A second reason is to group similar functions together for convenience, e.g:

// DOM functions are under myLib.dom
myLib.dom.someDOMFunction0;
myLib.dom.someDOMFunction2;
// Utility functions are under myLib.util
myLib.util.someUtilityFunction0;
myLib.util.someUtilityFunction0;

Note that the above has exactly the same chance of duplicates as similarly global variables:

myLib_dom_someDOMFunction0;
myLib_dom_someDOMFunction1;
myLib_util_someUtilityFunction0;
myLib_util_someUtilityFunction1;

Of course the former is generally preferred because it seen as easier to work with. I'm not advocating that you adopt the second approach (I use the first), just pointing out that while there is an issue with creating lots of global variables, so–called "global namespace pollution" is greatly overrated as a hazard.

lang-js

AltStyle によって変換されたページ (->オリジナル) /