Beginning ActionScript 3.0 – Summary of Functions

Sneak-Peek-128x128 Function, is a very important concept inside the ActionScript. And we talk a lot about the function in ActionScript 3.0. Let’s review them together now.

At first, we use an example to show you how the functions help you get rid of the redundancy of your code, and make your code easier to maintain.

And then, we discuss what the function is.

Function is a block of code; the behavior is deterministic, use for carry out specific task.

The following discussion is about the program flow and the definition of functions.

function function_name(parameter list) : return type {
// code block
return someValue;
}

The other way of defining the functions, function expression, is also introduced.

var function_name: Function = function (parameter list) : return_type {
//function body
}

After the function definition, is the function calling statements. In this section, we mainly talk about the parameter types, parameters’ sequence and some problems cause by them.

Follow the calling statements, I use a longer section try to explain you the parameter passing mechanism. It might be boring, but if you can understand the difference between the complex value and primitive value, you may avoid many traps. You just need to remember there exist assignments when you calling the function. The difference between primitive value and complex value is due to the different storing mechanisms of different type.

The next two sections are about the parameters.

The first one is about the default parameters. In ActionScript 3.0, you can declare default parameter values for a function. If a call to a function with default parameter values omits a parameter with default values, the value specified in the function definition for that parameter is used.

And there’re some problems you need to notice when you using the default parameter values, such as, the parameter with default values should be placed at the end of the parameter list, and default values should be known in the compile-time.

The second one is about the the Arguments object and the …(rest) parameter.

The arguments object is an array that includes all the parameters passed to the function. The arguments.length property reports the number of parameters passed to the function. The arguments.callee property provides a reference to the function itself.

The …(rest) parameter is similar to the arguments object without the callee property. If you don’t need the callee property of arguments object, you can use another parameter, the … (rest) parameter, instead of using the arguments object.

The arguments object is not available if any parameter is named arguments or if you use the … (rest)

After the parameters, we discuss the scope and nested functions.

Scope means the range in which a variable can be referenced; understand the scope chain can help you get a clear idea about the variables’ scope. And the variables’ covered is also introduced in this section.

The last section, we talk about how to declare a function inside another function, nested functions. And the scope and covered are also discussed in this topic.

It seems that we’ve talk a lot under functions. In fact we still have something uncovered, such as the relationship between the Function (the class), function statement and function expression. And the function closure is also not covered in this series. Actually, I think some topic might not suitable for the beginners, so I’m not going to talk about the topics in this series. If you’re interesting in those topics, please find some other books to read J

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

Leave a Reply