简体   繁体   中英

what is the best way to pass parameters in an as3 callback from js

I've been wondering this question for a while: in as3, when you call a method or a function, you pass it paramameters in this way:

method(param1, param2);

but what happends when your method requires a bunch of them? Instead of passing them all like: method(p1,p2,p3,p4,p5,p6,p7,p8);

a better way is to create a model object that contains all those params, and you just pass the whole model:

model : Model = new Model();
model.p1 = "something";
model.p2 = "another something";
...
method(model);

this way you can ensure that method() is called with the right number and type of parameters. but also avoiding use of a huge amount of parameters.

however, when using JS callbacks, two problems arises: passing the whole list of parameters: method(..., p8); is a bad way as always, but at least I have a strict way of forcing the callback to be called with the correct parameters. the other solution would be passing a model, but I can't pass from js a defined as3 Object, the most I can do is to pass a js object:

$("as3Component").as3Callback({p1 : "something", p2 : "another something"});

and from as3:

as3Callback(params : Object)
{
    trace(params.p1); 
}

however, I cannot have a strict way to know wich parameters I can receive, so Object could contain anything.

so my question is, what is the best way to pass a huge amount of parameters from js to as3 without having a single method with lots of parameters and not having this "magic" object that could contain any attributes on it. thx!.

Passing an object as a parameter is absolutely fine.

All you need is some sort of validation of the parameters, for example:

function as3Callback(params : Object):void
{
    params.p1 = params.p1 || "default value for p1";
    params.p2 = params.p2 || "default value for p2";
    params.p3 = params.p3 || "default value for p3";
    params.p4 = params.p4 || "default value for p4";

    ...

    trace(params.p1); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM