Const 及其影响
我开始在我的 AS3 代码中更多地使用 const
。我的理由是,如果一个值没有改变,那么为什么将其设为 var
。
我通过 Twitter 藤听说 const
值的优化方式与 vars
不同。由于推特简短而甜蜜,细节就到此为止。
有谁知道使用 var
和 const
的优点/缺点的更多信息。
让我首先声明一个缺点:在 Flash Builder 中,const 值在调试器中不可见。似乎不改变的值不值得在调试器中查看。
I've started using const
a lot more in my AS3 code. My reasoning being that if a value does not change then why make it a var
.
I heard through the twitter vine that const
values are optimized differently than vars
. Due to twitter being short and sweet, this is where the details stopped.
Does anyone have more info on the pro/cons of using var
and const
.
Let me start off by stating a con: in Flash Builder const values are not visible in the debugger. It seems values that do not change are not worth viewing in a debugger.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 Jackson Dunstan 的说法,使用 const 代替 var 时在性能方面没有真正的差异。
然而,存在相当大的语义区别,您自己指出了这一点。由于 const 无法更改,因此编译器会捕获错误更改值的任何问题。
According to Jackson Dunstan, there's no real difference performance wise when using const instead of var.
There's a pretty big semantical distinction however, which you point out yourself. A const, since it can't be changed, will make the compiler catch any issues where the value is changed in error.
我知道常量的唯一缺点是:您无法更改该值。就是这样。 ;-)
常量是常量,变量是变量。如果一个值是常量,那么它应该被声明为常量,反之亦然。这对编译器和开发人员来说是一个很大的提示。
如果将鼠标指针移到常量上,或者可以将常量作为表达式添加到“表达式”视图中,则 Flash Builder 会显示常量的值。当然,它们没有列在“变量”视图中。
有关更多信息,请阅读 AS3 文档:常量
The only con I know for constants is: you can't change the value. That's it. ;-)
Constants are constant and variables are variable. If a value is constant then it should be declared as constant and vice versa. That's a BIG hint for the compiler and the developer.
Flash Builder shows the value for constants if you move the mouse pointer over the constant or you can add a constant as expression to the "Expressions" view. Of course they are not listed in the "Variables" view.
Fore more informations read the AS3 Docs: Constants
关于您的缺点,如果您使用 Flash Builder,如果您转到视图上的菜单,选择 Flex 子菜单,然后选择显示常量,则常量在“变量”视图中可见。
Regarding your con, if you're using Flash Builder, constants are visible in the Variables view if you go to the menu on the view, select the Flex submenu, and chose Show Constants.