简体   繁体   English

angular2教程,const变量的样式指南

[英]angular2 tutorial , style guide for const variable

Accord Style guide angular2, tells: "Consider spelling const variables in lower camel case." Accord Style guide angular2,告诉:“考虑在较低的驼峰情况下拼写const变量。” "The tradition of naming constants in UPPER_SNAKE_CASE reflects an era before the modern IDEs that quickly reveal the const declaration. TypeScript itself prevents accidental reassignment" “在UPPER_SNAKE_CASE中命名常量的传统反映了现代IDE之前的一个时代,它快速揭示了const声明.TypeScript本身可以防止意外重新分配”

https://angular.io/styleguide#!#03-02 https://angular.io/styleguide#!#03-02

but in angular2 tutorial use for app.component.ts file (hero array), use UPPER_SNAKE_CASE 但是在用于app.component.ts文件(hero数组)的angular2教程中,请使用UPPER_SNAKE_CASE

    import { Hero } from './hero';
    export const HEROES: Hero[] = [
        {id: 11, name: 'Mr. Nice'},
        {id: 12, name: 'Narco'},
        {id: 13, name: 'Bombasto'},
        {id: 14, name: 'Celeritas'},
        {id: 15, name: 'Magneta'},
        {id: 16, name: 'RubberMan'},
        {id: 17, name: 'Dynama'},
        {id: 18, name: 'Dr IQ'},
        {id: 19, name: 'Magma'},
        {id: 20, name: 'Tornado'}
    ];

why? 为什么?

Imho there's a difference between naming a const containing a literal / a structure of literals or a const coming from variables: Imho在命名包含文字/文字结构的const或来自变量的const之间有区别:

I'd UPPER_SNAKE_CASE this: 我是UPPER_SNAKE_CASE这个:

const API_URL = "http://www.example.com";

And lowerCamelCase this: 而lowerCamelCase这个:

for (var i = 0; i < 10; i++) {
  const maxHeightOfVehicle = bridges[i].height - 10;

  console.log(maxHeightOfVehicle);
}

Although maxHeightOfVehicle is technically a constant, it doesn't mean that it won't have different values over time. 虽然maxHeightOfVehicle在技​​术上是一个常量,但并不意味着它不会随着时间的推移而具有不同的值。

API_URL on the other hand is a literal making it a "real constant"; 另一方面, API_URL是一个文字,使其成为“真正的常数”;

So although it's not needed anymore to prevent you from changing a constant, it still tells you something about what you're dealing with. 因此,虽然不再需要它来阻止你改变常量,但它仍然会告诉你一些关于你正在处理什么的东西。

So in the tour of heroes case I'd prefer keeping HEROES in UPPERCASE because it's a structure of literals. 所以在英雄之旅中我更喜欢将HEROES保持在大写中,因为它是一个文字结构。 a well-defined constant. 一个明确定义的常数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM