Flash as learning: Deep BROADCASTERMX

Source: Internet
Author: User
Tags format empty functions

Asbroadcaster can't see the original file, this is similar to that, only a little different, you can study how the broadcast is achieved.
If you look at this post in a strange format, you can see it in a clear format.

The following are program code:
Class Mx.transitions.BroadcasterMX {
#include "version.as"
Version information
private Var _listeners:array;
_listeners List
static function Initialize (O:object, Dontcreatearray:boolean) {
/*
static method Initialize (), used to turn parameter o into an event source
Dontcreatearray This parameter if set to True does not create a _listeners array for O, I do not know why this argument,
It doesn't seem to be creating a _listeners array for O, and broadcasting is useless. Who knows why, want to explain to me.
*/
if (o.broadcastmessage!= undefined) Delete o.broadcastmessage;
If the object o already exists broadcastmessage, delete the
O.addlistener = Mx.transitions.BroadcasterMX.prototype.addListener;
Make O object have AddListener method, see behind Knowledge point 1
O.removelistener = Mx.transitions.BroadcasterMX.prototype.removeListener;
Make O object have RemoveListener method, see behind Knowledge point 1
if (!dontcreatearray) o._listeners = new Array ();
_global. Assetpropflags (O, "addlistener,removelistener,_listeners", 1);
Hide Addlistener,removelistener,_listeners
}
function AddListener (o:object): number {
Fill in the listener, give the listener a Broadcastmessage method, and fill him in the Broadcast subscription list _listensrs
This.removelistener (o);
if (this.broadcastmessage = = undefined) {
This.broadcastmessage = Mx.transitions.BroadcasterMX.prototype.broadcastMessage;
_global. Assetpropflags (This, "Broadcastmessage", 1);
}
return This._listeners.push (o); here's the length of the array.
}

function RemoveListener (o:object): Boolean {
Remove a listener from the list of broadcasters
var a:array = this._listeners;
var i:number = a.length;
Here is not used for, is the list of benefits, delete O, if the list is empty, the Broadcastmessage also deleted
while (i--) {
if (a[i] = = O) {
A.splice (i, 1);
if (!a.length) this.broadcastmessage = undefined;
return true;
}
}
return false;
}

function Broadcastmessage (): Void {
Emit broadcast function
var e:string = String (Arguments.shift ());
Converts the first argument passed into a string type, which should be a function name, see the Knowledge point 2
var A:array = This._listeners.concat ();
Returns a copy of the array when the Concat method argument is empty (see Help)
var l:number = a.length;
for (var i=0; i<l; i++) a[i][e].apply (a[i), arguments);
Go through the list of functions, and pass the parameters, apply the usage see Knowledge point 3
}
};

Knowledge Points: 1.
There are two ways to fill a custom class in AS1:
A: Written in a function

The following are program code:
function A () {
This.myfunc = function () {Trace ("A.myfunc");}
}

B: Written on the prototype-prototype of the function

The following are program code:
function B () {
}
B.prototype.myfunc = function () {
Trace ("B.myfunc");
}


In the AS2:

The following are program code:
Class a{
function haha () {
Trace ("...")
}
}

Where is this haha method written? After the H person to mention point, looked at ASV, the answer is really h people said, written on the prototype, quite with B's writing.
Knowledge Points: 2.
When you call a function, a Argments object is automatically generated inside the function, which is an array of all the arguments you pass in the function.
Arguments.shift () is the first element of the array.
Knowledge Points: 3.
Apply is a method of the function class, which means that all function has this method. This method makes a function work in another domain, does not know the description is not---B See examples

The following are program code:
A = {};
a.tostring = function () {
Return "I am a";
};
function functions () {
Trace (this);
Trace (arguments);
}
Functions (1, 2, 3);
Output: _level0 1,2,3
function. apply (_root, [1, 2, 3]);
Output: _level0 1,2,3
function. Apply (A, [1, 2, 3]);
Output: I am a 1,2,3

Get it, man.

for (var i=0; i<l; i++) a[i][e].apply (a[i), arguments);

Look at this is the last traversal in the class, traverse the list, a[i] for each element in the listener list, A[i][e] is the E method for each object, A[i][e].apply (a[i), or the A[i] method in the A[i][e field, and don't forget to pass the argument over, that's all. A[i][e].apply (A[i], arguments). Okay, there's a problem. Who answered ~ Thank you
Slept for 88



Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.