在function()中使用静态变量 [英] Use static variable in function()
问题描述
我想知道我们是否可以像在JavaScript中那样在函数中声明一个静态var。
I would know if we can declare a static var in a function, as we can do in JavaScript.
当我回调函数时,我的变量将她保留在最后
When I callback my function, my variable keep her last affectation.
还是我只能使用全局变量(这不是性感的...)?
Or can I only use global variable (it's not sexy...) ?
推荐答案
您不能在函数中使用static。
You can't use static in a function.
Dart中的全局变量没有代码味道,因为它们只是库全局。
JavaScript中的全局变量很丑陋,因为它们可能与全局变量冲突来自第三方图书馆。
这在Dart中不会发生。
Global variables in Dart are no code smell because they are only library global.
Global variables in JavaScript are ugly because they can conflict with global variables from 3rd-party libraries.
This doesn't happen in Dart.
因为您可以在Dart中建立一个所需的小图书馆(例如只有一个变量),并且在导入库时,您拥有类似于库名称空间的东西
As you can make a library in Dart as small as you want (for example only one variable) and you have something similar to a namespace for a library when you import it like
import 'my_globals.dart' as gl;
然后像
print(gl.myGlobalValue);
这没有代码味。
您还可以创建一个类来模拟名称空间,例如
You could also create a class to simulate a namespace like
class MyGlobals {
static myVal = 12345;
}
但是Dart首选使用库全局变量,而不是仅包含静态变量的类或功能。
But library global variables are preferred in Dart instead of classes which contain only static variables or functions.
这篇关于在function()中使用静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!