Flash ActionScript Advanced Tips: Deep understanding of Function (2)

Source: Internet
Author: User
Advanced | skills

Oh, the last post everyone reflects well, a few days by several Web sites reproduced. Http://www.webjx.com/htmldata/2006-06-15/1150354262.html

Talk less, this time to continue the last topic. Look at the popularity situation, if you like me this advanced technical topic, then will release some of my own over the years in the project development notes in practical skills.

function return function: There is a Proxyobject object, we want to use the content of the proxy object to determine a method to handle the MyObject object. When Proxyobject is a string, we also want to determine the return of a different function (or method) based on the contents of its string. These function parameters and types are not exactly the same, some may be a parameter, some may be multiple parameters, different types. The traditional solution: calling other functions inside a function is too much. Even if it is resolved, it is not as simple as the choosefuncby below me.

Disadvantages of course, that is more flexible, you must clearly manage each objective function and judgment logic. Because this flexible programming method compiler is unable to check, cannot detect the type does not match this kind of error. Be careful with your rookie.

Call through a afunc () with only one argument:
Choosefuncby ("a") ("a Func has only one parameter.");
Output: Afunc (): A func has only one parameter.
Bfunc () with two parameters called by B:
Choosefuncby ("B") ("b func has two parameters.", "No.2 parameter");
Output: Bfunc (): B func has two parameters. One more parameter:no.2 Parameter
String mismatch, default function
Choosefuncby ("Wu Lala") ("I Choose A function");
Output: Welcome to kingda.org! My blog
var withobj:object = new Object ();
var myobj:object = {name: "Black Feather", blog: "http;//www.kingda.org", Hobby: "StarCraft"};
Choosefuncby (Withobj) (myobj);
/* Output:
Objectfunc ():
Name: Black Feather
blog:http;//www.kingda.org
Hobby:starcraft
*/
function Choosefuncby (inputstring): function {

Use one: Use the kind of parameter to determine the returned function
if (!) ( typeof (inputstring) = = "string") {
return objectfunc;
}

Use two: return function based on parameter content
Switch (inputstring) {
Case "A":
return afunc;
Case "B":
return bfunc;
Default
return kingdafunc;
}
More extended application: Using the number of parameters, instanceof to determine the different class instances to select functions and so on
}
function Afunc (ns:string): Void {
Trace ("Afunc ():" + NS);
}
function Bfunc (ns:string, np:string): Void {
Trace ("Bfunc ():" + NS + "One more Parameter:" + NP);
}
function Kingdafunc (): Void {
Trace ("Welcome to kingda.org! my blog ");
}
function Objectfunc (kingdaobj:object): Void {
Trace ("Objectfunc ():");
for (var i in kingdaobj) {
Trace (i + ":" + kingdaobj[i]);
}
}

As was said last time, the function is also an object, and the function object can also add properties and methods dynamically.
Function Add properties dynamically

Using a function dynamic property to calculate the number of function calls
Of course you can use this technique to do more and more useful things, I just started to start. :)

A game in which there is a firing function shot (). I'd like to know how many shots were fired in total, so you can use the shot function's property times.

Overall, this is a less good programming habit for AS2.0, albeit very handy. However, because the use of dynamic properties makes it impossible for the compiler to perform type checking, it also increases the likelihood of errors in large items.
For AS1.0, instead of using global variables such as _root, _level1, then I would recommend that you use this programming technique. It is always safer than global variables, and it is not easy to produce conflicts and cobweb effects.

Note that when referencing the dynamic properties of the shot function, This.times cannot be used within the function, and that does not point to the properties of the function itself. You can only use function names plus attributes, for example: Shot.times

Shot.times = 0; Initializing times
Shot ()//output: Shot (): times:1
Shot ()//output: Shot (): Times:2
Shot ()//output: Shot (): Times:3
Function shot (): Void {
Shot.times + +;
Trace ("Shot (): Times:" + shot.times);
Shot's other code is here.
}

Method for dynamically adding function objects
By the way, it's more interesting to add a method.
The function object is not like a normal class instance, nor is it like a function. This technique can make our functions very powerful and make them more complex and difficult to manage.
Two-edged sword, you weigh yourself to do it.
My advice is that AS1.0 can learn a little bit more about this technique, AS2.0 users should try to use standard solutions. In the small range can be used for convenience, and in no way agree with the large-scale use of large scale.

Shot.times = 0;
Shot.reload = function () {
Trace ("Reload:" + this.times);
if (This.times >3) {
This.times = 0;
}
}
Shot.reload ();
Shot ();
Shot ();
Shot ();
Shot ();
Shot ();
Shot ();
Function shot (): Void {
Shot.times + +;
Trace ("Shot (): Times:" + shot.times);
Shot.reload ();
Shot's other code is here.
}
/* Output:
reload:0
Shot (): times:1
Reload:1
Shot (): Times:2
Reload:2
Shot (): Times:3
Reload:3
Shot (): times:4
Reload:4
Shot (): times:1
Reload:1
Shot (): Times:2
Reload:2
*/

At the end of the article: it can be seen that the use of dynamic add properties and methods of the technique, you can make the function of this particular thing exceptionally strong. And its flexibility is unprecedented, just imagine if the dynamically added method can return a function (see the first tip). Do not forget that the dynamically added method can directly access the function input parameters, then how many of its derivative techniques? How can a function become a powerful programming object? There is nothing to do but to imagine. This is the infinite flexibility that the function shows us.
However, I would like to say that skill is ultimately a skill, it has its two sides. Flexibility is its advantage and its disadvantage. The use of a small scope makes you very refreshing; large-scale uses of large projects, unless you manage well, will cause you to get a splitting headache.
Once you've read through the AS2.0 of all the exposed classes, you'll find that Macromedia programmers use these techniques as well. As long as the management is good, this is definitely the advantage of ActionScript.
AS2.0 Coder, happy, cautious!
(End of full text)
P.S someone asked me why not write argument, callee, caller and other aspects of the skills, my idea is that these in the help write very clearly, we can see for themselves. And these related skills, the article has a lot to see its popularity has not been considered advanced skills, so no longer repeat!



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.