且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

人们为什么在某些情况下会使用变量?

更新时间:2021-12-09 00:49:47

有多种原因

  1. 'const`变量将防止该值意外更改,例如如果您编写了 closure ,其中包含在直接作用域之外可以访问的变量.

  1. A 'const` variable will prevent the value from accidentally changing, e.g. if you have written a closure that includes a variable that is accessible outside immediate scope.

JavaScript引擎尚不能常见的子表达式消除,这是一种非常常见的编译器优化.使用临时变量可以提高性能,例如在此示例中.

Javascript engines are not yet capable of common subexpression elimination, which is a very common compiler optimization. Using a temporary variable could improve performance, like in this example.

有时,使用不同名称的变量可以阐明功能;请参见编写真正明显的代码(ROC).示例:

Sometimes a variable with a different name can clarify the functionality; see Writing Really Obvious Code (ROC). Example:

var activeUsername = User.name;  //It's not just a user's name, it's the active user's name!

  • 有时,额外的变量使调试/监视变量更加容易

  • Sometimes that extra variable makes it easier to debug/watch variables

    有时候您想在分配过程中设置一个单独的断点

    Sometimes you want to set a separate breakpoint during the assignment

    我锻炼过度吗?

    Am I overracting?

    是的