dart函数范围
示例
Dart函数也可以匿名声明或嵌套声明。例如,要创建嵌套功能,只需在现有功能块中打开一个新功能块
void outerFunction() {
bool innerFunction() {
///做东西
}
}该函数innerFunction现在可以在内部使用,并且只能在内部使用outerFunction。没有其他任何功能可以访问它。
Dart中的函数也可以匿名声明,通常用作函数参数。一个常见的例子是对象sort方法List。此方法采用带有以下签名的可选参数:
int compare(E a, E b)
文档指出,0如果a和b相等,则该函数必须返回。它返回-1ifa<b和1ifa>b。
知道了这一点,我们可以使用匿名函数对整数列表进行排序。
List<int> numbers = [4,1,3,5,7];
numbers.sort((int a, int b) {
if(a == b) {
return 0;
} else if (a < b) {
return -1;
} else {
return 1;
}
});匿名函数也可以绑定到标识符,例如:
Function intSorter = (int a, int b) {
if(a == b) {
return 0;
} else if (a < b) {
return -1;
} else {
return 1;
}
}并用作普通变量。
numbers.sort(intSorter);
热门推荐
4 合字的祝福语简短
10 初升高祝福语家长简短
11 送个姐妹生日祝福语简短
12 新年祝福语和语句简短
13 别人过大寿祝福语简短
14 对女友的简短祝福语
15 航空公司祝福语简短
16 比赛结束花束祝福语简短
17 永远漂亮祝福语女生简短
18 妹妹生日祝福语简短唯美