About the delegate class, you can find it in the installation directory of Flash. Delegate as the name implies is the agent, hehe is not as the name implies, should be translation, haha. This class is to agent, hehe, then how to Agent? Here's an example:
1, create a new FLA document, and then define a function in the first frame of the script:
function Showheart ()
{
Trace ("I love U, my Little Leaf");
}2, then create an MC, create a function in the first frame of this MC, and use the proxy call, which is code:
function Showagain ()
{
Trace ("This are show Again Function");
Trace ("this=" + this);
This.showheart ();
}
Static methods that use proxies
var fam:function = mx.utils.Delegate.create (_root, Showagain);
It's like creating a function pointer, and then calling,
FAM (); Then you will find that its output is:
This are show Again Function
This=_level0
I love you, my Little leaf Note: Don't forget to drag the MC to the main timeline, hehe, I just forgot, by the way.
From this example, you can see: What delagate is doing, it seems to have created a function pointer, but at the same time it has changed the function of the upper and lower questions. But it simply changes the context of the function that the fam refers to, and the actual Showagain function does not change the context. Do not believe you can call Showagain to see. At the end of the first frame of MC, add a second call:
Showagain (); Look at the output:
This are show Again Function
This=_level0
I Love U, my Little Leaf
This are show Again Function
The different points of This=_level0.instance1 are:
The former: this=_level0 the latter this=_level0.instance1. This reference is not the same.
The former: I love you, my Little Leaf the latter: empty (without printing that is not called)
It is obvious that the problem is clear when you think about it.
But the question here is, how did delegate do it? So let's see what else is delegate:
function Delegate (f:function): This is a constructor, passing in a function.
CreateDelegate (Obj:object): function: Pass in an Object to return a function.
So it seems. You can also use the instantiated method to use delegate, the method that you just used is static method!
Then modify the code in the first frame of the MC:
function Showagain ()
{
Trace ("This are show Again Function");
Trace ("this=" + this);
This.showheart ();
This.a_txt.text = "KKK";
}
var fam:function = mx.utils.Delegate.create (_root, Showagain);
FAM ();
Showagain ();
var mydgt:mx.utils.Delegate = new Mx.utils.Delegate (showagain);
var fam:function = mydgt.createdelegate (_root);
FAM (); output result:
This are show Again Function
This=_level0
I Love you, my Little leaf and the first few changes, this should be the delegate class application bar, hehe more superficial, is this pull. Ha ha. But I can't help but think that my last post on function class can be sure that the two methods of delegate class and function class (call, apply) must be related. Next Discussion Oh!
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.