Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, this isn't specifically a module design pattern as I know it, this is a general constructor pattern. The module pattern I know is a singleton, but here you could have many instances of Foo(). That being said...</p> <p>Q: How important is it to initialize variables when they're declared (i.e. at the top of Foo)</p> <p>Declaring them at the top is important for clarity, but initializing them isn't as important here since you're doing so in the init. If you weren't doing this, initializing them prevents you from having to do an undefined check before testing the variable later:</p> <pre><code>var x; function baz(){ if (typeof(x) === 'undefined') { // init } else { if (x &gt; 0) { blah } else { blah blah } } } </code></pre> <p>Q: How might I re-initialize part of this object if a client of this Module gets to a state that it's foo object needs to be set back to it's originals</p> <p>Is there something wrong with creating a public reset method? It will have access to the private variables.</p> <pre><code>function Foo() { // ... this.reset = function () { privateNumber = 0; // etc }; // ... } </code></pre> <p>Q: What sort of memory issues might this design lead to and how can I refactor to mitigate that risk?</p> <p>I don't know.</p> <p>Q: Where can I learn more? Are there any articles that address this without relying too much on the latest and greatest of EcmaScript 5 ?</p> <p>Here's a good read about the Javascript module (and other) pattern(s): <a href="http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript">http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload