Advanced | skills
What exactly is a function?
We are accustomed to the existence of function, just as we are accustomed to the air we breathe without exploring its essence. It seems that function and number, Boolean, string are all types of ActionScript.
First look at the following code:
Trace (Afunc); Output: [Type Function]
Trace (Afunc instanceof Function); Output: True
Trace (Afunc instanceof Object); Output: True
function Afunc () {
Trace ("This is afunc! Excuted! ");
}
The first line tells us that the type of Afunc is a function, and the second line confirms that Afunc is indeed an instance of a function type, and a third row is more interesting to tell us that Afunc is an object.
Beginners may be surprised, in fact AS2.0 in addition to the original data type number, Boolean, string,undefined, NULL, the rest is all object. And AS3.0 is more extreme, everything is object. Including the original data type is object, but is a special invariant object (immutable objects) type. It's a long way from the subject. :D
What kind of object is a function in essence?
Unlike other programming languages, in as, a function is an object and can have independent properties or even methods. Like Arguments,callee,caller. In AS3.0, the function is also made up of more properties.
Once the function is executed, a special object is established. We call it "active object", which contains the above attributes and local variables. This object is inaccessible to us and belongs to the built-in mechanism. Also, each function contains a built-in chain of scopes (scope chain), which is also set up to allow Flash Player to check all declarations. Functions can be nested in layers, and so is the scope chain. The largest range chain that is of course the scope chain of the global function, including all global variables and functions.
The first line tells us that the type of Afunc is a function, and the second line confirms that Afunc is indeed an instance of a function type, and a third row is more interesting to tell us that Afunc is an object.
Beginners may be surprised, in fact AS2.0 in addition to the original data type number, Boolean, string,undefined, NULL, the rest is all object. And AS3.0 is more extreme, everything is object. Including the original data type is object, but is a special invariant object (immutable objects) type. It's a long way from the subject. :D
What kind of object is a function in essence?
Unlike other programming languages, in as, a function is an object and can have independent properties or even methods. Like Arguments,callee,caller. In AS3.0, the function is also made up of more properties.
Once the function is executed, a special object is established. We call it "active object", which contains the above attributes and local variables. This object is inaccessible to us and belongs to the built-in mechanism. Also, each function contains a built-in chain of scopes (scope chain), which is also set up to allow Flash Player to check all declarations. Functions can be nested in layers, and so is the scope chain. The largest range chain that is of course the scope chain of the global function, including all global variables and functions.
What if the function is object?
will bring us great convenience and the change of programming thinking:
Use one: This is simple to use, set up an agent function object, according to the different conditions, it points to different functions, to achieve dynamic change. It is believed that experienced programmers understand the convenience of dynamically changing functions. And because as provides this convenience, the use of this feature can derive a lot of skills.
var kingdafunc:function;
var sex:string = "Male";
if (sex = = "Male") {
Kingdafunc = Malefunc;
} else {
Kingdafunc = Femailfunc;
}
Kingdafunc (); Output: I am a Boy
function Malefunc () {
Trace ("I am a Boy");
}
function Femalefunc () {
Trace ("I am a Girl");
}
Use two: Create a function execution queue.
For example, I have an object and I want to do a series of operations on it based on different situations. But sometimes it takes all the action, and sometimes it just needs a part of it. Then this higher level of skill, can guarantee the high reusability of the Code and concise.
var funcary:array = new Array ();
Add required action steps to the queue
Funcary.push (Afunc);
Funcary.push (Bfunc);
Funcary.push (Cfunc);
Object for Operation
var originobject:object = new Object ();
A few steps need to be taken, determined by the Execqueue parameter, which may be dynamically determined in the actual engineering application.
var execqueue:number = funcary.length;
/core Step:/function Queue execution. It can be packaged into a function, or an instance of a class, in practical use.
for (Var i:number =0 I
Funcary[i] (originobject);
}
Trace out the contents of the Originobject after performing the operation
for (var i in Originobject) {
Trace (i + ":" + originobject[i]);
}
Operation Steps A,b,c
function Afunc (eo:object) {
Eo.afuncexected = true;
Trace ("Afunc ()");
}
function Bfunc (eo:object) {
Eo.bfuncexected = true;
Trace ("Bfunc ()");
}
function Cfunc (eo:object) {
Eo.cfuncexected = true;
Trace ("Cfunc ()");
}
The output reads:
Afunc ()
Bfunc ()
Cfunc ()
Cfuncexected:true
Bfuncexected:true
Afuncexected:true
The first three lines indicate that a,b,c three functions are executed sequentially. The following three lines indicate that Orginobject did go through three steps and had three more properties of true.
Black Feather Reminder: skills can be further extended!
You can use a function to manage the position of each element in the queue to change the order of the operation function. For example, to arrange the call order by an array
var operationary:array = [2,1,0]
for (var i:number = 0; I
Funcary[operationary[i]] (originobject);
}
The function then performs the operation by 2,1,0 such a reverse order.
This technique also has a lot of places to extend, such as dynamic control of the parameters of the operation function, and so on, for everyone to study the expansion.