Flash / Flex / ActionScript/Function/function parameters
Содержание
- 1 All arguments that have a default value must all be placed last in the order of arguments
- 2 Complex data types, like Array, pass values by reference.
- 3 Function definition with multiple arguments
- 4 Handling an Unknown Number of Parameters
- 5 Mix the named parameter and unnamed parameters
- 6 Passing Parameters by Value or by Reference
- 7 Passing Parameters to a function
- 8 Primitive data types are passed by value
- 9 Setting Default Values: add an equal sign (=) and the default value after an argument name
- 10 The rest parameter can also be used with other required parameters.
- 11 Using the Rest Argument (...): represents a variable number of arguments as an array followed by a name for that array
All arguments that have a default value must all be placed last in the order of arguments
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
}
function storeAddress(name:String, email:String, zip:String = null){
}
}
Complex data types, like Array, pass values by reference.
The following data types are passed by value:
String
Number
int
uint
Boolean
Null
void
All other data types are passed by reference.
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var person:Object = {name:" P Gibbons"};
capitalizeEmployeeName(person);
trace(person.name); // Displays: PETER GIBBONS
}
function capitalizeEmployeeName(employee:Object):void {
if (employee.name != null) {
employee.name = employee.name.toUpperCase();
}
}
}
}
Function definition with multiple arguments
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
displayGreeting("Joey", "Good morning.");
}
function displayGreeting(sFirstName:String, sGreeting:String):void {
trace("Hello, " + sFirstName + ". " + sGreeting); //Hello, Joey. Good morning.
}
}
}
Handling an Unknown Number of Parameters
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
trace(getAverage(1,2,3));
trace(getAverage(1,2,3,4));
trace(getAverage(1,2,3,4,5));
}
public function getAverage (...numbers) {
var total = 0;
for (var i = 0; i < numbers.length; i++) {
total += numbers [i];
}
return total / numbers.length;
}
}
}
Mix the named parameter and unnamed parameters
public function initializeUser (name, ...hobbies) {
}
initializeUser("H", "v", "s");
name is assigned the value "H", and hobbies is assigned the value ["v", "s"].
Passing Parameters by Value or by Reference
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var myVariable:Number = 5;
var myVariablePlusOne:Number = incrementByOne(myVariable);
trace(myVariable);
trace(myVariablePlusOne);
}
function incrementByOne(nValue:Number):Number{
nValue++;
return nValue;
}
}
}
// 5
// 6
Passing Parameters to a function
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
displayGreeting("Joey"); // Displays: Hello, Joey
displayGreeting("Robert"); // Displays: Hello, Robert
}
function displayGreeting(sFirstName:String):void {
trace("Hello, " + sFirstName);
}
}
}
Primitive data types are passed by value
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var effort:Number = 110;
var trimmedPercentage:Number = limitPercentage(effort);
trace(effort, "%");
trace(trimmedPercentage, "%");
}
function limitPercentage(percentage:Number):Number {
percentage = Math.min(percentage, 100);
percentage = Math.max(percentage, 0);
return percentage;
}
}
}
Setting Default Values: add an equal sign (=) and the default value after an argument name
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
showGreeting("Mr. Whimsy");
showGreeting();
}
function showGreeting(name:String = "stranger"):void {
trace("Hello, "+ name + ", nice to meet you.");
}
}
}
The rest parameter can also be used with other required parameters.
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
// trace(sum()); // throws a runtime error.
trace(sum(1,2,3,4,5)); // Displays: 15
}
function sum(base:Number, ... numbers):Number {
var result:Number = base;
for each (var num:Number in numbers) {
result += num;
}
return result;
}
}
}
Using the Rest Argument (...): represents a variable number of arguments as an array followed by a name for that array
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
trace(sum(1,2,3,4,5)); // Displays: 15
}
function sum(... numbers):Number {
var result:Number = 0;
for each (var num:Number in numbers) {
result += num;
}
return result;
}
}
}