Jon Crawford Jon Crawford - 14 days ago 5
Javascript Question

Combining javascript psuedo-class objects under a single namespace

I have some classes I ported from Java to javascript. I would like to combine them in a library.

for example:

var myLibrary = {
version: 1.0,
...more code...
};


now a class I would like to add to this library (this was ported from Java to JavaScript!) follows:

//tickerClass defines a stock-like ticker
//author: Jonathan Crawford Nov 2016
//license: none, just please credit me if you use my code
//version 3.0
//usage:
//construct
//var testTicker = new jTicker(viewLength,spaceSize);
//add a message
//testTicker.addMessage(number,message);
//this will get increment the start index and return the ticker
//var temp = testTicker.getTicker();
//call that in a timed loop and stick it wherever
//to remove/change
//if(testTicker.keyExists(number);){testicker.removeMessage(number);}
//then add the updated version
//testTicker.addMessage(number,newMessage);
//message numbers need not be added in order(but will display in order)
//numbers may be skipped, i.e 0,1,5,6,9 is a valid key list

//testTicker.removeMessage(number);
var jTicker = function(length,spaces) {
//make space even
if (spaces%2 != 0) { spaces += 1; }
//set space size]
this.spaceSize = spaces;
//set length
this.viewLength = length;
//start position
this.position = 0;
this.key = 0;
//all messages
this.messages = {};
}
//add message
jTicker.prototype.addMessage = function(key,msg) {
var halfspace = "";
var half = this.spaceSize / 2;
for (var i = 0; i < half; i++) { halfspace += "\u00A0"; }
var temp = halfspace + msg + halfspace;
this.messages[key] = temp;
}
//remove message
jTicker.prototype.removeMessage = function(key) {
delete this.messages[key];
}
//does key exist?
jTicker.prototype.keyExists = function(key) {
return (key in messages);
}
//get string snipit
jTicker.prototype.getTicker = function() {
this.position += 1;
//set pointer to next key when end of current message reached
var stop = false;
for (key in this.messages) {
if (stop) { //execute once
this.key = key;
stop = false; //if this does not run we can fix it
break; //or key will match and it will excute again
}
else {
// only execute when key matches and position is past end of message
if (key == this.key && this.position > this.messages[this.key].length-1) {
this.position = 0;
stop = true; //set flag for an execution on next key
}
}
}
while(stop){ //if new key never set
for (key in this.messages) {
this.key = key; //we want the first key
stop = false;
break;
}
}
var temp = this.messages[this.key].substring(this.position);
var work = false;
for (key in this.messages) {
//starting at next key
if (work) { temp += this.messages[key]; }
//work rest of keys
if(key == this.key){ work = true; }
}
//contiue if short
for (key in this.messages) {
temp += this.messages[key];
if (temp.length == this.viewLength) { break; }
}
//return exactely enough
return temp.substring(0,this.viewLength-1);
}


this class works as beautifully as is, but encapsulating it is proving difficult. I would like to do something like this:

var ticker = new myLibrary.jTicker(30,10);
var otherObj = new myLibrary.object2();


The problem seems to be I can't add the prototype stuff. It's like I need to define them outside the encapsulation. Is it even possible to include them inside? I understand I could just put the prototype stuff as inner functions and use the this.that = this hack, but I would rather not.

An example of the Library format I would like to use can be found here: http://crawfordcomputing.com/js/html5Jetris.js

The reason for this question is that I have broken my Jetris into 6 or so classes in Java that I can easily port to JavaScript, (like the jTicker class) and I would like to combine them all in the same Library namespace.

Answer

Use an IIFE

var myLibrary = (function(){

   var jTicker = function(length,spaces) {
    //make space even
    if (spaces%2 != 0) { spaces += 1; }
    //set space size]
    this.spaceSize = spaces;
    //set length
    this.viewLength = length;
    //start position
    this.position = 0;
    this.key = 0;
    //all messages
    this.messages = {};
   }

   // you can have all your prototype code here here

   return{
      version:"1.0",
      jTicker:jTicker
   };

})();

usage

var myInstance = new myLibrary.jTicker(10,30);