JavaScript Audio Player Notes
Patterns
I’m using a weird combination of the module pattern and function prototypes. This lets me declare most methods as just functions scoped inside the module, while still having a new-able object to create. I like the idea of adding methods to the prototype because it only happens once.
var ClassFactory = function() {
var instance = function() {
};
instance.prototype = {
publicMethod: function() {
privateMethod(this);
}
};
function privateMethod(instance) {
// do something with instance
};
return {
create: function() {
return new instance();
}
};
}();
Useful logging function
function log() {
if (console && console.log) {
console.log.apply(console, arguments);
}
}
YUI 2
I’m not only using YUI 2 for the project, but an old version, and you can really feel it. It’s pretty cranky. But I took care to limit the usage of expensive functions and to cache what I could and I think it’ll work just fine.
Event Delegation
I’m only adding a single DOM event to the container of the audio player, and I use event delegation to determine what click handler to pass the event to. It works fine on such a simple example, but I really feel like there must be a way to do it easier. I’d love to be able to match an event target to a CSS selector. I feel like jQuery’s live events does this but I can’t quite parse through that code.
YUI Doc
Easier to install than I thought it would be. Has several python dependencies though. It doesn’t really map to my weird module/class/instance pattern very well, but it also doesn’t actually read the code, just the comments, so you can write whatever you want to make it more specific. I’ll definitely add it to the project first and make sure to write all those “@method” keywords from the start.