Splitting up a large class with modules

user3886234

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.

So I let's say I have a class like

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?
}

The problem is that I have a large amount of methods using various ways of declaring them ( this.method, var method, this.prototype.method). What I'm wondering is if there is a relatively simple way to create another module(s) and require them into MyClass as part of the class definition in order to increase readability.

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).

I've only been learning JS for a few weeks now, so go easy on me if I'm being idiotic. Thanks in advance :)

--edit--

Blindly playing around with it, I've figured out how to do what I'm looking for with prototype and public methods.

//Underneath MyClass definition
require('./prototype-methods')(MyClass);
require('./public-methods')(MyClass);

Then the gist of it in other files is:

module.exports = function(MyClass) {
  MyClass.prototype.method = . . . .

  MyClass.method = . . . .
}

So that leaves me wondering if there is a way to do something similar with private methods. Any ideas?

---edit2---

What exactly is you use case? Could you give an example of what your class does?

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 score class, which acts as a mediator between the part, effect and player classes. The part class is just a wrapper for an instrument, which is an object with functions that play music.

The namespace has a factory function that returns a new instance of score, which in turn has a factory functions that return new part(s), effect(s), and eventually the player. Also, there can be more than 1 score at any given time so I could potentially make a playlist.

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.

The specific class this question is about is the part 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).

user3886234

I've found that I can pretty much add everything except for private members to MyClass with a module by passing MyClass to the module as a parameter.

It seems that I could technically add / access private members using eval, but I've been told it is evil. Just taking the prototype and public methods out of the file trimmed it down significantly, so leaving the private members in there is perfectly fine.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related