PDA

View Full Version : Function.params



K2xL
June 16th, 2007, 09:43 PM
Hey guys,

I was wondering if anyone knew if it was possible to declare a function variable like so:


var f:Function = ReferenceToSomeFunctionInClass;


And assign parameters to the function? I was hoping I could do something like:


var f:Function = ReferenceToSomeFunctionInClass;
f.params[0] = "First parameter!";
f();


It'd be very helpful if this was possible; I don't like resorting to


var f:Function = ReferenceToSomeFunctionInClass;
f.call(this,"First Parameter!");

pingnak
June 17th, 2007, 01:58 AM
If you look up the Function class, you'll find that it has a couple of functions, and one of them, Function.apply passes an array of parameters.

So if you have...

function Blah( x:int, y:int, z:int ):int
{
return x+y+z;
}

var Array:array = [1,2,3];
var result:int = Blah.apply(this, array );

FizixMan
June 17th, 2007, 08:36 AM
Note that this will run the function in the scope of "this", wherever it's called. If you want to activate the function in the scope of the object/class that contains it, pass that object into the function:

var myClassObject:myClass = new myClass();
var myFunction:Function = myClassObject.myFunctionName;

myFunction.apply(myClassObject, [1, 2, 3]);

TheCanadian
June 17th, 2007, 12:18 PM
No, it won't. Class methods are bound to the class and will always be called in the scope of the instance which contains them. For example:

package {
public class Test extends Object {
public function method():void {
trace(this);
}
}
}

var t:Test = new Test();
var f:Function = t.method;
f.apply(this); //[object Test]

FizixMan
June 17th, 2007, 08:33 PM
So what's the pointing of passing "this", or any other object as a parameter of Apply()?

TheCanadian
June 17th, 2007, 10:24 PM
For functions that aren't bound to a class.

var o1:Object = {func:function():void {trace(this == o1, this == o2); }}
var o2:Object = new Object();

o1.func(); //true, false ~ called in o1
o1.func.call(o2); //false, true ~ called in o2

FizixMan
June 17th, 2007, 11:39 PM
Oh. That makes sense.

Krilnon
June 18th, 2007, 12:05 AM
Well, if we take K2xL literally ('ReferenceToSomeFunctionInClass'), then it might also be worth pointing out that you can have properties that are functions in a class that you can treat like methods, except their scope isn't bound to the class instance because the functions aren't methods. They are just what TheCanadian was talking about, but this is a more specific case that is somewhat interesting to think about.


var klassInstance:Klass = new Klass();
klassInstance.funktion(); // [object Klass]
klassInstance.funktion.call(this); // [object Tester] (or whatever the enclosing class is)


package {
public class Klass {
public var funktion:Function = function():void {
trace(this);
}
}
}

Also, since the function call operator '()' can't be redefined, it's doubtful that you'd be able to choose arguments like your (K2xL's) second code block was attempting to do.

K2xL
June 20th, 2007, 12:57 AM
I just want to know if I can assign parameters to a function variable without actually calling a function.

For example, let's say I had something like passing a function to another function.



public function DoSomething()
{
var a:Function = trace;
a.params[0] = "Hey! Is it possible to do this? If not, wouldn't it be a great feature?!";
Animate(a);
}
public function Animate(OnDoneAnimating:Function)
{
// blah blah blah some random animation or code
OnDoneAnimating(); // or OnDoneAnimating.call(this), or OnDoneAnimating.apply(this)
}

This is something that would be VERY useful in my opinion... I mean isn't it unfair to assume I'll know what parameters I will want to pass in on the fly like that?

Basically, and I would like someone to verify, the code I just wrote above would never work due to function types not allowing the storage of parameters?

Rezmason
June 20th, 2007, 01:11 AM
If you look up the Function class, you'll find that it has a couple of functions...

You're clearly right, but... techincally, how does that even work? If you've got a function, and it has a method, and that method's techincally a function, then the method has a method as well, as does the method's method, and the method's method's method, ad infinitum.

Does Flash give the methods of the Function class special treatment?

Dimitree
June 20th, 2007, 08:33 AM
No, it won't. Class methods are bound to the class and will always be called in the scope of the instance which contains them. For example:
ActionScript Code:

package {
public class Test extends Object {
function method():void {
trace(this);
}
}
}




ActionScript Code:

var t:Test = new Test();
var f:Function = t.method;
f.apply(this); //[object Test]





Sorry to bring back this issue but to work this example do I have to place
ActionScript Code:

import.flash.display.*



in the class?

TheCanadian
June 20th, 2007, 10:21 AM
no

And it's not possible K2xL.

Dimitree
June 20th, 2007, 10:48 AM
1180: Call to a possibly undefined method addFrameScript.
5000: The class 'Test' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.

I get these errors

TheCanadian
June 20th, 2007, 10:55 AM
Don't link it to a movie clip in the library. Create an instance using the new operator.

Dimitree
June 20th, 2007, 11:22 AM
Don't link it to a movie clip in the library. Create an instance using the new operator.


You mean a new instance of the Test class?
I wrote

var t:Text=new Text ();



still get these errors...sthg I do wrong I guess....

Krilnon
June 20th, 2007, 04:05 PM
You're clearly right, but... techincally, how does that even work? If you've got a function, and it has a method, and that method's techincally a function, then the method has a method as well, as does the method's method, and the method's method's method, ad infinitum.


Functions are (first-class) objects as well, so it makes sense that they can have methods, no? In some other languages (like Java), functions aren't first-class objects, which may be why it seems unusual to you.

TheCanadian
June 20th, 2007, 07:16 PM
You mean a new instance of the Test class?
I wrote

var t:Text=new Text ();



still get these errors...sthg I do wrong I guess....
The class is called Test, not Text.

Rezmason
June 20th, 2007, 11:37 PM
Functions are (first-class) objects as well, so it makes sense that they can have methods, no? In some other languages (like Java), functions aren't first-class objects, which may be why it seems unusual to you.

Well, since Actionscript has been my primary language for a long while now, I'm pretty much used to it. :smirk: Still, no matter what type of object functions are in AS3, the concept that every method is an object with a method leads to the logical conclusion that every Flash program has an infinitude of methods. So there has to be some sort of cutoff point.

Krilnon
June 21st, 2007, 12:23 AM
Well, there are memory limits, etc., but the same thing can be done with any object, not just functions. For example, every object has a constructor property, including the constructor property itself. There's no real reason to cut it off ever, as the internal property accessor just supplies a reference over and over again. Saying that there would be an infinite amount of functions/methods in a program is a bit odd unless you're actually generating new functions, instead of returning the same references over and over.

Rezmason
June 21st, 2007, 12:28 AM
Right, now I got it. :)

dthought
June 21st, 2007, 02:17 AM
Assigning values to a function's parameters? Why all the complexity? The answer is simple! You don't want a function - you want an instanced class.

Classes encapsulate both methods (functions) and properties (variables), and you can use get and set methods to manipulate those properties. Then, when you call a method of the class, it can use the values that you have set.

If you're just trying to shorten your code by using references, you're being a bit naughty... I'm all for clever code, but readability and debugging goes to hell quite easily if you start using all these references all over the place. Keep it simple, even if that means more typing. It'll be much more beneficial in the long run.

Hahaha... quite a funny post... :)

Dimitree
June 21st, 2007, 04:33 AM
The class is called Test, not Text.

Ok I feel like an idiot:toad:... Thanks Canadian for your help

TheCanadian
June 22nd, 2007, 02:31 AM
Haha, no worries :)

rahul_7star
June 22nd, 2007, 02:36 AM
how to coliide with object having star shape cause movie clip take rctangle box