0

Can i create multiples functions inside for loop?

var mySound1 = new buzz.sound( "laser-01", { formats: [ "ogg", "mp3", "acc" ]});
var mySound2 = new buzz.sound( "alien-noise-01", {formats: [ "ogg", "mp3", "acc" ]});

var sound = [mySound1, mySound2]

// additional sounds
var $i;
for ( $i = 0; $i< sound.length; $i++){
    function playsound[$i](){   
           a[$i].play().fadeIn().loop();
        }
}

playsound1();
2
  • what is a ? do you mean sound[$i] ? Commented Dec 3, 2012 at 3:25
  • 1
    Function names have to be an identifier, so that syntax won't work at all. Also, because all those hypothetical functions will share the same variable $i, they won't do what you need. This is a variation on a question asked thousands of times on Stackoverflow. Commented Dec 3, 2012 at 3:28

2 Answers 2

3

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(); 
Sign up to request clarification or add additional context in comments.

6 Comments

Upvoting this... it's a good suggestion, even though it's not exactly what the OP asked for.
I looked at the problem, and this is the solution that would work, instead of creating a different function for each sound. I could have given a solution to his problem, but this is better, IMO :)
Better just to define your function as buzz.sound.prototype.playSound = function() { this.play().fadeIn().loop(); } before the var sounds declaration, no?
Yeah, that's why I gave you an upvote. It's a more graceful solution than what he requested. Try using my suggestion above to simplify it.
@TheSmose, yes, perhaps you are right :) but then again, is new buzz.sound actually returning an object with buzz.sound.prototype prototype?
|
1

You'd be better off just passing $i as a parameter to the playsound function

var sounds = [
    new buzz.sound( "laser-01", { formats: [ "ogg", "mp3", "acc" ]}),
    new buzz.sound( "alien-noise-01", {formats: [ "ogg", "mp3", "acc" ]})
];

var playsound = function (i) {   
   sounds[i].play().fadeIn().loop();
}

playsound(1);

And if you really want the playsound1() style function name, you could eval it (though I suggest against adding this):

for (var i = 0; i < sounds.length; i++){
    eval('var playsound' + (i+1) + ' = function() { playsound(' + i + '); };');
}

playsound1();
playsound2();

1 Comment

Cheers @max for the checkmark. I want to reiterate that I think you should use Yanick's answer below. It's not exactly what you wanted, but it's a cleaner solution.

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.