How to pass a function as parameter in Action Script?

Asked

Viewed 96 times

2

Can anyone tell me what kind of object allows me to make a value matrix and a function similar to the code below and script 3 action? If it were in Java you could do something like this:

My Interface:

package test;

public interface MyInterface {

    public void alert();
}

And my class with the use of Interface:

package test;

import java.util.HashMap;
import java.util.Map;

public class MyClass {

    public static void main(String[] args) {
        Map<Integer, MyInterface> map = new HashMap<>();

        map.put(1, new MyInterface() {
            @Override
            public void alert() {
                System.out.println("valor 1");

            }
        });

        map.put(2, new MyInterface() {
            @Override
            public void alert() {
                System.out.println("valor 2");

            }
        });
    }

}
  • I don’t understand. The function already exists in that class?

  • There is. I wanted to pass any function with argument from my matrix understood? can be something like that too.. map.put(value, Alert("test"));

  • I removed my answer because the question is now another. I had not realized that it was from the Java Map that you were talking about. I’ll leave it to someone who knows more Java answer...

  • No cara se Voce conhecer algum objeto que eu possa usar ou outra forma, ajuda ae @bfavaretto

  • Dude, my Java is not the best, and my AS3 is pretty rusty, I haven’t used it in years. But I tried a new answer, see if it works.

1 answer

2

You can try using an array instead of the HashMap, and literal objects as value:

var map = [];
map.push({
    alert: function() { trace("aaaaa") }
});
map.push({
    alert: function() { trace("bbbbb") }
});
map[0].alert(); // aaaaa
map[1].alert(); // bbbbb
  • Beauty brother, I’ll test anything here I tell you =D

  • I thought of some alternatives, but I did not find a better one than this, rsrs. Your AS is rusty not!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.