声明一个新函数。这可以用在几种上下文中:
// in the global scope function functionname([parmlist]) [: type] { [body] } // declares a method in a class [attributes] [modifiers] function functionname([parmlist]) [: type] { [body] } // declares a method in an interface [attributes] [modifiers] function functionname([parmlist]) [: type]
- attributes
可选项。控制方法的可见性和行为的属性。
- modifiers
可选项。控制方法的可见性和行为的修饰符。
- functionname
必选。函数或方法的名称。
- paramlist
可选项。一个用于函数或方法的以逗号分隔的参数列表。每个参数都可以包含一个类型规范。最后一个参数可以是 parameterarray,它由三个句号 (...) 后接一个参数数组名称和一个类型化数组的类型批注来表示。
- type
可选项。方法的返回类型。
- body
可选项。定义函数或方法如何操作的一个或多个语句。
使用 function 语句来声明一个以后要使用的函数。在脚本的其他地方调用该函数前,body 中包含的代码不被执行。return 语句用于从函数返回值。您不必使用 return 语句,因为程序将在到达函数结尾时返回。
方法类似于全局函数,不同之处在于它们的范围限于定义它们的 class 或 interface,并且它们可以有各种修饰符来控制它们的可见性和行为。interface 中的方法不能有主体,而 class 中的方法必须有主体。此规则有一个例外情况;如果 class 中的某个方法为 abstract,或 class 为 abstract,则该方法就不能有主体。
可以用类型批注声明函数或方法返回何种数据类型。如何将返回类型指定为 void,则函数内的任何 return 语句都不返回值。如果指定了 void 以外的任何返回类型,则函数内的所有 return 语句都必须返回一个强迫为指定返回类型的值。如果指定了返回类型,但 return 语句没有带任何值或者当到达函数结尾时没有出现 return 语句,则返回 undefined 值。构造函数不能指定返回类型,因为 new 运算符自动返回创建的对象。
如果没有为函数指定显式返回类型,则返回类型将设置为 Object 或 void。只有当没有 return 语句或 return 语句在函数体中不带任何值的情况下,才选择 void 返回类型。
参数数组可以用作函数的最后一个参数。在必选参数之后传递给函数的任何其他参数(如果有的话)都将输入到该参数数组中。参数的类型批注不是可选项;它必须是一个类型化数组。若要接受任意类型的参数,请使用 Object[] 作为此类型化数组。当调用的函数可接受各种参数时,应使用一个所需类型的显式数组,而不是提供一个参数列表。
在调用函数时,请确保始终包括圆括号和任何必选参数。不带圆括号就调用函数将导致返回函数的文本,而不是函数的结果。
示例 1下面的示例阐释了第一个语法中 function 语句的用法:
interface IForm { // This is using function in Syntax 3. function blank() : String; } class CForm implements IForm { // This is using function in Syntax 2. function blank() : String { return("This is blank."); } } // This is using function in Syntax 1. function addSquares(x : double, y : double) : double { return(x*x + y*y); } // Now call the function. var z : double = addSquares(3.,4.); print(z); // Call the method. var derivedForm : CForm = new CForm; print(derivedForm.blank()); // Call the inherited method. var baseForm : IForm = derivedForm; print(baseForm.blank());
该程序的输出为:
25 This is blank. This is blank.示例 2
在本示例中,printFacts 函数将输入内容视为 String,并使用一个参数数组来接受各种 Objects。
function printFacts(name : String, ... info : Object[]) { print("Name: " + name); print("Number of extra information: " + info.length); for (var factNum in info) { print(factNum + ": " + info[factNum]); } } // Pass several arguments to the function. printFacts("HAL 9000", "Urbana, Illinois", new Date(1997,0,12)); // Here the array is intrepeted as containing arguments for the function. printFacts("monolith", [1, 4, 9]); // Here the array is just one of the arguments. printFacts("monolith", [1, 4, 9], "dimensions"); printFacts("monolith", "dimensions are", [1, 4, 9]);
该程序在运行时显示下列输出:
Name: HAL 9000 Number of extra information: 2 0: Urbana, Illinois 1: Sun Jan 12 00:00:00 PST 1997 Name: monolith Number of extra information: 3 0: 1 1: 4 2: 9 Name: monolith Number of extra information: 2 0: 1,4,9 1: dimensions Name: monolith Number of extra information: 2 0: dimensions are 1: 1,4,9要求
版本 1(对于语法 1) .NET 版本(对于语法 2 和 3)
请参见参考
new 运算符class 语句
interface 语句
return 语句
概念
变量和常数的范围类型批注
类型化数组