Skip to main content
Coreydwillis
Known Participant
February 25, 2013
Answered

Variable Function Names

  • February 25, 2013
  • 2 replies
  • 472 views

What I'm trying to do is add event listeners for each position up to the total of compPositions. This way, each one will have a dymanic function name. Right now, they all launch function "clicked1" but I can't figure out how to make it add numbers to the function name dynamically. Is there a different method I should be using to accomplish this goal?

Right now, compPosition = 5 so it should create 5 event listners, but I want them each to have their own function they exucute.

          var i;

          for (i = 1; i < compPositions +1; i++)

          {

                    currentPos = "pos" + i;

                    this["pos" + i].addEventListener(MouseEvent.CLICK, clicked1)

          }

Edit:

Actually, after thinking about it, I think this is a better approach, but I'm not sure how to pass over a variable using a click event.

            var i;

            for (i = 1; i < compPositions +1; i++)

            {

                  var currentPos; //now it's private to this for loop

                  currentPos = "pos" + i;

                  this["pos" + i].addEventListener(MouseEvent.CLICK, posClicked)

              }

In this version, I just need to pass over the currentPos variable to the function, but I'm not sure if that's possible.

Thanks,

Corey

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer Nabren

Before solving, I would caution why you need them to have a unique function/unique function name. You should be able to have them all hit the same function and do different logic within depending on the target.

That aside, you could do something like this:

for (i = 1; i < compPositions +1; i++)

{

          currentPos = "pos" + i;

          this["pos" + i].addEventListener(MouseEvent.CLICK, this["clicked" + i])

}

However, you would have to manually define each function. Alternatively, you can use closures:

for (i = 1; i < compPositions +1; i++)

{

  currentPos = "pos" + i;

  this["pos" + i].addEventListener(MouseEvent.CLICK, getFunction(i))

}

function getFunction(i:int):Function

{

          return function(e:MouseEvent):void { trace("Clicked " + i); };

}

EDIT:

Using the closure method you could also pass the current pos if you needed to:

for (i = 1; i < compPositions +1; i++)

{

  currentPos = "pos" + i;

  this["pos" + i].addEventListener(MouseEvent.CLICK, getFunction(i, currentPos))

}

function getFunction(i:int, currentPos:String):Function

{

          return function(e:MouseEvent):void { trace("Clicked " + i + " currentPos = " + currentPos); };

}

2 replies

Nabren
NabrenCorrect answer
Inspiring
February 25, 2013

Before solving, I would caution why you need them to have a unique function/unique function name. You should be able to have them all hit the same function and do different logic within depending on the target.

That aside, you could do something like this:

for (i = 1; i < compPositions +1; i++)

{

          currentPos = "pos" + i;

          this["pos" + i].addEventListener(MouseEvent.CLICK, this["clicked" + i])

}

However, you would have to manually define each function. Alternatively, you can use closures:

for (i = 1; i < compPositions +1; i++)

{

  currentPos = "pos" + i;

  this["pos" + i].addEventListener(MouseEvent.CLICK, getFunction(i))

}

function getFunction(i:int):Function

{

          return function(e:MouseEvent):void { trace("Clicked " + i); };

}

EDIT:

Using the closure method you could also pass the current pos if you needed to:

for (i = 1; i < compPositions +1; i++)

{

  currentPos = "pos" + i;

  this["pos" + i].addEventListener(MouseEvent.CLICK, getFunction(i, currentPos))

}

function getFunction(i:int, currentPos:String):Function

{

          return function(e:MouseEvent):void { trace("Clicked " + i + " currentPos = " + currentPos); };

}

Coreydwillis
Known Participant
February 25, 2013

I see exactly what you're talking about now. The closures are a much better method. Thanks! :-)