pylint  常量 大写_何时大写JavaScript常量

pylint  常量 大写

Many JavaScript style guides suggest capitalizing constant names. Personally, I rarely see this convention used where I thought it should be. This was because my definition of a constant was a bit off. I decided to do a bit of digging and become a bit more familiar with this convention.

许多JavaScript样式指南建议大写常量名称。 就个人而言,我很少看到该约定在我认为应该使用的地方使用。 这是因为我对常量的定义有些偏离。 我决定进行一些挖掘,并对这个约定更加熟悉。

我们如何定义“常数”一词? (How do we define the term “constant”?)

In programming, a constant it something that doesn’t change.

在编程中,常量是不变的。

It is a value that cannot be altered by the program during normal execution.

它是程序在正常执行期间无法更改的值

So, does JavaScript gives us a way to declare a value that can’t be changed? Before we answer this, let’s look at the roots of this convention.

那么,JavaScript是否提供了一种声明不能更改的值的方法? 在回答这个问题之前,让我们看一下该约定的根源。

大写约定源于C (The capitalization convention has roots in C)

C is a compiled language. This means that another program converts all of your code into machine code before it runs.

C是一种编译语言。 这意味着另一个程序在运行之前将所有代码转换为机器代码。

JavaScript, on the other hand, is an interpreted language. An interpreter reads your code, line-by-line, as it runs.

另一方面,JavaScript是一种解释语言。 解释程序在运行时逐行读取您的代码。

The difference between compilation and interpretation plays a role in how we declare constant values in C.

编译和解释之间的差异在我们如何使用C声明常量值中起作用。

In C, I can declare a variable like this:

在C语言中,我可以这样声明一个变量:

int hoursInDay = 24;

int hoursInDay = 24;

Or, a constant like this:

或者,像这样的常量:

#define hoursInDay 24

#define hoursInDay 24

The second example is called a symbolic constant. Symbolic constants can be a sequence of characters, a numeric constant, or a string. These are also called primitive values. The primitive values in JavaScript are strings, numbers, booleans, null, undefined, symbol (not to be confused with symbolic constants) and big int.

第二个示例称为符号常量 。 符号常量可以是字符序列,数字常量或字符串。 这些也称为原始值。 JavaScript中的原始值是字符串,数字,布尔值,null,未定义,符号(不要与符号常量混淆)和big int。

Now, let’s revisit compilation.

现在,让我们回顾一下编译。

Before compilation, there is a pre-compilation phase. Here, the pre-compiler replaces all instances of symbolic constants with the respective value. The compiler never knows that the programmer wrote hoursInDay. It only sees the number 24.

在编译之前,有一个预编译阶段。 在此,预编译器将符号常量的所有实例替换为相应的值。 编译器永远不知道程序员写了hoursInDay 。 它只看到数字24

Capitalization helps the programmer see these truly constant values.

大写有助于程序员看到这些真正恒定的值。

#define HOURS_IN_DAY 24

#define HOURS_IN_DAY 24

JavaScript常数不同于符号常数 (JavaScript constants are different than symbolic constants)

Before ES6, we stored most values in variables, even those values that you wanted to remain constant.

在ES6之前,我们将大多数值存储在变量中,甚至包括您希望保持不变的那些值。

Capitalization helped us see values we wanted to remain constant.

资本化帮助我们看到了我们希望保持不变的价值。

var HOURS_IN_DAY = 24;
var hoursRemaining = currentHour - HOURS_IN_DAY;
var MY_NAME = 'Brandon';
MY_NAME = ... // oops don't want to do this.

ES6 introduced the declaration const which isn’t a “constant” in the purest sense.

ES6引入了声明const ,从最纯粹的意义上说,它不是“常数”。

ES6 added the terms const and let as ways to create variables with different intentions.

ES6添加了术语constlet作为创建具有不同意图的变量的方式。

With those two terms, you may think that we either:

有了这两个术语,您可能会认为我们要么:

  1. don’t need to capitalize anything since we can clearly see which variables are intended to remain the same, or

    不需要大写,因为我们可以清楚地看到哪些变量打算保持不变,或者
  2. we should capitalize everything that we declare with const.

    我们应该将用const声明的所有内容都大写。

By definition, const creates a constant that is a read-only reference to a value. This does not mean the value it holds is immutable. It only says that the variable identifier cannot be reassigned.

根据定义, const创建一个常量,该常量是对值的只读引用。 这并不意味着其持有的价值是不变的。 它仅表示不能重新分配变量标识符

In other words, some const references can change.

换句话说,某些const引用可以更改。

const firstPerson = {
  favoriteNumber: 10,
};
const secondPerson = firstPerson;
console.log(secondPerson.favoriteNumber); //10
firstPerson.favoriteNumber +=1;
console.log(secondPerson.favoriteNumber); //11

The above example shows that the declaration const doesn’t ensure that the variable is immutable.

上面的示例表明声明const不能确保变量是不可变的。

const only prevents us from trying to reassign the variable name. It doesn’t stop the object property from changing. Remember: objects are pass-by-reference.

const仅阻止我们尝试重新分配变量名称。 它不会阻止对象属性的更改。 请记住:对象是按引用传递的。

// "TypeError: Assignment to constant variable."secondPerson = 'something else';
const secondPerson = 'Me'
secondPerson = 'something else';

So, for JavaScript, we have to go beyond merely looking for a const declaration. We need to ask two questions to determine if a variable is a constant:

因此,对于JavaScript,我们不仅要查找const声明。 我们需要问两个问题以确定变量是否为常数:

  1. Is the value of the variable primitive?

    变量原语的值是吗?
  2. Do we intend to keep the variable name pointing at the same value throughout our program?

    我们是否打算在整个程序中使变量名指向相同的值?

If the answer is yes to both, we should declare the variable with const and may capitalize the name.

如果对两者的回答都是肯定的,则应使用const声明变量,并可以将名称大写。

Notice I said “may.” The spirit of this convention comes from different languages that had actual constants. JavaScript doesn’t. At least in the purest sense. This may be why you see this convention less often than you might expect. Airbnb has a great section in their style guide with their take here.

请注意,我说“可以”。 该约定的精神来自具有实际常量的不同语言。 JavaScript没有。 至少在最纯粹的意义上。 这可能就是为什么您很少看到此约定的原因。 Airbnb在其风格指南中特别介绍了这些内容。

The key takeaway is to recognize defining a constant in JavaScript has to include the programmer's intentions.

关键要点是要认识到在JavaScript中定义常量必须包括程序员的意图。

In addition, not every convention from one language makes sense in a different language. Finally, I can only imagine many conventions were used long before IDEs had the capabilities they have today. I’m convinced my IDE takes pleasure in telling me I’m wrong. It happens a lot.

此外,并非所有来自一种语言的约定都适用于另一种语言。 最后,我只能想象到在IDE具有今天所拥有的功能之前就已经使用了许多约定。 我确信我的IDE很高兴告诉我我错了。 它经常发生。

Thanks for reading!

谢谢阅读!

woz

沃兹

Follow me on Twitter.

Twitter上关注我

笔记 (Notes)
  • You may wonder why I didn’t use PI in any of these examples. Acronyms– especially two-letter acronyms–tend to be either always capitalized or always lowercase by convention.

    您可能想知道为什么在这些示例中都没有使用PI 。 首字母缩略词(尤其是两个字母的首字母缩略词)习惯上总是大写或小写。

翻译自: https://www.freecodecamp.org/news/when-to-capitalize-your-javascript-constants-4fabc0a4a4c4/

pylint  常量 大写

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值