You can re-use a function :
// declare your sound dictionary
var sounds = {
'laser': new buzz.sound( "laser-01", { formats: [ "ogg", "mp3", "acc" ]}),
'alien-noise': new buzz.sound( "alien-noise-01", {formats: [ "ogg", "mp3", "acc" ]})
};
// this is the helper function
var playSoundFn = function() {
this.play().fadeIn().loop();
};
// assign the helper function to all your sounds
for (var i=0, len=sounds.length; i<len; i++){
sounds[i].playSound = playSoundFn;
}
// then play your sounds from any of them in your dictionary :
sounds['laser'].playSound();
sounds['alien-noise'].playSound();
** Edit ** (thanks to TheSmose)
If each item in the sounds array are created with the buzz.sound.prototype prototype, then you can simply add a custom function to it and simply use it :
// this is the helper function
buzz.sound.prototype.playSound = function() {
this.play().fadeIn().loop();
};
// declare your sound dictionary
var sounds = {
'laser': new buzz.sound("laser-01", { formats: ["ogg", "mp3", "acc"]}),
'alien-noise': new buzz.sound("alien-noise-01", {formats: ["ogg", "mp3", "acc"]})
};
// then play your sounds from any of them in your dictionary :
sounds['laser'].playSound();
sounds['alien-noise'].playSound();
a? do you meansound[$i]?$i, they won't do what you need. This is a variation on a question asked thousands of times on Stackoverflow.