Hello, Kirupa community! This is my first post here.
I’m interesting in following methods:
Function.apply ();
Function.call ();
I’ve never used it before and confused a little when saw examples in help.
I tried to copy Function.call () example in my custom class, but errors occurs. I tried to search on forum and site but with no result. Is any tutorial exists to understand their purpose.
You don’t have to use these functions you know. They’re there for specific needs you may have, you’re not supposed to use them randomly without knowing what they do and watch what happens.
Anyway, a good place to start would be to check the AS Dictionary on Function.apply and [URL=“http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary370.html”]Function.call. They should give you a good idea of what these functions do.
Yeah, I know about language reference, but it not friendly sometimes. I never used this methods so I thought it could be useful and I tried to understand them. That what I found (maybe it’ll help somebody):
class Test_1{
private var c:Number = 100;
public function seeObj ():Void{
trace (c);
}
}
class Test_2{
private var c:Number = 200;
}
class Main{
public static function main():Void{
var a:Test_1 = new Test_1 ();
a.seeObj ();//output: 100
var b:Test_2 = new Test_2 ();
a.seeObj.call(b);//output 200
}
}
So it works like Delegate, but between instanses…
Sort of, yes. They simply take the function and apply it as a method of the thisObject passed as the first parameter, and using the parameters further specified in the second parameter (and onward in the case of Function.call). Your example illustrates perfectly how it can be used.
I never used this methods so I thought it could be useful and I tried to understand them.
That’s a pretty good attitude to have, I think.
So it works like Delegate, but between instanses…
The Delegate class uses apply
:
static function create(obj:Object, func:Function):Function
{
var f = function()
{
var target = arguments.callee.target;
var func = arguments.callee.func;
return func.apply(target, arguments);
};
f.target = obj;
f.func = func;
return f;
}