title: Splitting up a large class with modules
body: <p>I'm attempting to build my first (browser) library, and chose to use the node-style commonjs modules + browserify to organize my code.  I've structured it so each module contains 1 class, which worked out pretty well until a few of the classes started getting eye-bleedingly huge.</p>
  
  <p>So I let's say I have a class like</p>
  
  <pre><code>module.exports = MyClass;
  
  function MyClass() {
    //initializing stuff
  
    this.publiceMethod= function publicMethod() {
      //do stuff
    };
  
    var privateMethod = function privateMethod() {
      //do stuff
    };
  }
  
  MyClass.prototype.notSureMethod = function notSureMethod() {
    //err... static method?
  }
  </code></pre>
  
  <p>The problem is that I have a <em>large</em> amount of methods using various ways of declaring them ( <code>this.method</code>, <code>var method</code>, <code>this.prototype.method</code>).  What I'm wondering is if there is a relatively simple way to create another module(s) and require them into <code>MyClass</code> as part of the class definition in order to increase readability.</p>
  
  <p>The ideal behavior that I'm looking for is something like a module that is a self-executing function that shares the same scope of what it's called from (including private variables / methods if possible).</p>
  
  <p>I've only been learning JS for a few weeks now, so go easy on me if I'm being idiotic.  Thanks in advance :)</p>
  
  <p>--edit--</p>
  
  <p>Blindly playing around with it, I've figured out how to do what I'm looking for with prototype and public methods.</p>
  
  <pre><code>//Underneath MyClass definition
  require('./prototype-methods')(MyClass);
  require('./public-methods')(MyClass);
  </code></pre>
  
  <p>Then the gist of it in other files is:</p>
  
  <pre><code>module.exports = function(MyClass) {
    MyClass.prototype.method = . . . .
  
    MyClass.method = . . . .
  }
  </code></pre>
  
  <p>So that leaves me wondering if there is a way to do something similar with private methods.  Any ideas?</p>
  
  <p>---edit2---</p>
  
  <blockquote>
    <p>What exactly is you use case? Could you give an example of what your
    class does?</p>
  </blockquote>
  
  <p>I'm making a library for the web audio api that essentially lets you compose music.  I have a namespace for the library, and the namespace (at the moment) contains a few different classes.  There is the <code>score</code> class, which acts as a mediator between the <code>part</code>, <code>effect</code> and <code>player</code> classes.  The <code>part</code> class is just a wrapper for an <code>instrument</code>, which is an object with functions that play music.</p>
  
  <p>The namespace has a factory function that returns a new instance of <code>score</code>, which in turn has a factory functions that return new <code>part</code>(s), <code>effect</code>(s), and eventually the <code>player</code>.  Also, there can be more than 1 score at any given time so I could potentially make a playlist.</p>
  
  <p>I initially tried to use a purely functional pattern, but my code took on a new meaning of spaghetti.  I'm more familiar with namespacing / classes than I am functional patterns.  Overall, I've been building the library just as I would a normal javascript file, but due to the added complexity, have been using commonjs modules + browserify to easily split the code into different files / build the parts.</p>
  
  <p>The specific class this question is about is the <code>part</code> class.  I'm wanting the library to be very flexible in what notations it accepts, so I needed to add quite a few methods to account for all of those notations (= big file).</p>