0

I want to create a js class that resembles simple music playlist (Array). I want to instantiate this playlist with IDs, each ID being a track ID in my database. I have interface like this:

function Playlist() {
    Playlist.prototype.current = 0;
    Playlist.prototype.prev = function() {
        if (this.current-1 < 0) {
            return null;
        }
        return this[--this.current];
    };
    Playlist.prototype.next = function() {
        if (this.current+1 >= this.length) { // length is index + 1
            return null;
        }
        return this[++this.current];
    };
    Playlist.prototype.seek = function(id) {
        for (i in this) {
            if (this[i] == id) {
                this.current = parseInt(i);
                return i;
            }
        }

        return false;
    };
    Playlist.prototype.getCurrent() {
            return this.current;
    };
};

The code above DOES NOT do what I want, because I imagine it as class that has it's method defined, that can be instantiated like this:

var newPlaylist = Playlist(2,3,5,10/* those are my ids */);

And currently the only way I've found is something like:

Playlist.prototype = new Array(2, 3, 5, 10/* those are my ids */);

Which does not make any sense since it can be instantiated as different objects. Any ideas are very welcome!

1

2 Answers 2

2

Best way - nested array;

function Playlist() {
    this.current = 0;
    this.list = Array.prototype.slice.call(arguments);;
};

Playlist.prototype.prev = function() {
    if (this.current-1 < 0) {
        return null;
    }
    return this.list[--this.current];
};
Playlist.prototype.next = function() {
    if (this.current+1 >= this.list.length) { // length is index + 1
        return null;
    }
    return this.list[++this.current];
};
Playlist.prototype.getCurrent = function() {
    return this.current;
};

var newPlaylist = new Playlist(2,3,5,10/* those are my ids */);

But you can't use list[i] to get element by index, but you just need add at() method to your class that provide similar functionality

PlayList.prototype.at(i) {
    return this.list[i];
}
Sign up to request clarification or add additional context in comments.

Comments

1

Since you cannot subclass Array, you should build wrapper objects with your Playlist constructor:

Playlist = (function() {
    function Playlist(list) {
        this.list = list || [];
    }

    Playlist.prototype.current = 0;
    Playlist.prototype.prev = function() {
        if (this.current <= 0)
            return null;
        return this.list[--this.current];
    };
    Playlist.prototype.next = function() {
        if (this.current+1 >= this.length)
            return null;
        return this.list[++this.current];
    };
    Playlist.prototype.seek = function(id) {
        return this.list.indexOf(id);
    };

    return Playlist;
})();

Usage:

var newPlaylist = new Playlist([2,3,5,10]);

4 Comments

The only thing that I did not like is that I can not have those definitions as single unit (function). Probably thats the best I could get with JS.
Have a look at the module pattern for building code in "units".
I read this article: adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth still, I'm not quite sure how the above code would look like implemented using this. I know it's quite too much but would you be so kind to show me the same example with the module pattern? Thanks a ton!
There's not much in here (as you don't really need it), but I've adapted the answer to show you the code sample.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.