So I have a pseudo class with functions and vars inside of it. For example:
function MyClass()
{
this.a = 0;
this.b = function(c)
{
this.a += c;
}
}
Then, when I go to use it later I’ll do this:
var myObject = new MyClass();
myObject.b(3);
myObject.b(5);
but when I do this:
console.log("A: " + myObject.a);
I get:
A: 0
What am I doing wrong?
Here’s my actual code. It’s split into mutiple files but I’ll put up the ones that are relevant:
function SongDatabase() {
this.songs = new Array();
this.loadSongs = function () {
};
this.saveSongs = function () {
};
var _Constructor_ = function () {
var mySong = new Song();
this.songs = new Array(mySong);
};
_Constructor_();
}
function LyriX() {
var songDatabase = new SongDatabase();
//var playlistDatabase = new PlaylistDatabase();
songDatabase.loadSongs();
var sourceList = new ScrollableList();
sourceList.setElement($S.getElement("sourceList"));
var accessoryList = new ScrollableList();
accessoryList.setElement($S.getElement("accessoryList"));
var sourceListClick = function (index) {
$S.log("source click: " + index);
if (index == 0) {
displaySongs();
}
};
sourceList.setClickListener(sourceListClick);
var displaySongs = function () {
$S.log("display songs");
// STACK OVERFLOW LOOK HERE!!! thanks :)
// in debug in chrome songDatabase.songs is a zero length array
accessoryList.loadArray(songDatabase.songs);
};
}
$S.addOnLoadListener(function () {
new LyriX();
});
I see someone’s taking the “Java” in JavaScript too literally. 🙂
A couple things you should know about JavaScript:
In JavaScript, arrays don’t work like they do in other languages.
They’re a lot more like dictionaries then what you would call an array in C or Java;
they’re not significantly more memory efficient, or faster;
no preallocation is done;
there’s no offeset, etc, in the low-level implementation.
JavaScript arrays are little more than a convenient (but useful!) structure
for holding order-imperative data.
Arrays can be created using the
new Array(length)expression,or the simple literal expression,
[].Generally, you’ll want to use the array literal,
[].Using
new Array(length)doesn’t really do anything useful;it sets the initial
lengthproperty of the array, but that’s basically it.All elements remain
undefined.There are no additional constraints or bounds checking.
You can do
a[100] = 'whatever'on an array created by callingvar a = new Array(5)and the interpreter won’t bat an eye.
JavaScript uses prototypal inheritance which is significantly different then the
classical inheritance model used in languages like C++ and Java.
With these points in mind, lets examine the following code block:
This block of code is probably not doing what you think it’s doing.
SongDatabasemethods inside theSongDatabase()functionyou’re creating new method functions for every instance of
songDatabase.This may not be a big deal when you’re dealing with a couple of dozen of instances,
but if you’re dealing with hundreds, the extra memory required can become a problem.
You’ll want to use the
prototypepattern here, instead (see below)._Constructor_function isn’t doing anything.var mySong = new Song()creates a newmySongobjectlocal to the
_Constructor_function and not accessible outside of it.When the
_Constructor_invocation returns,it’s
mySongvariable is garbage collected (like any other local variable would be)._Constructor_is a private function and not a method;I’m not entirely sure what
thisin that context will reference.You may end up creating a songs property on the global object
(but I’d want to test that to be sure).
Array()with thenewoperator,it takes an optional argument that sets the initial
lengthof the array.In this case, the interpreter will try to coerce
mySonginto a number(
mySongis not added to the array!);when that fails it will simply return a new array with
length = 0.Instead, you’re better off writing
SongDatabase()like so:The prototypal pattern may look strange,
but its probably the best way to handle your use case.
You’ll still have direct access to then
songsarray (which may or may not be important),and by attaching the
loadSongsandsaveSongsfunctions toSongDatabase‘s prototypeyou ensure that those functions are shared.