常量和配置文件在JavaScript项目结构中的区别是什么?

huangapple go评论68阅读模式
英文:

What is difference between constants and configs file in structure of project JavaScript?

问题

我对React.js还很陌生,所以对项目结构中常量和配置文件之间的区别有些困惑。据我所知,常量包含在项目中无法更改并可重复使用的值,例如:

// constants.js

const APPLE = 'APPLE'; 
const BANANA = 'BANANA';

但是configs文件似乎也与constants做了相同的事情,例如:

// configs.js 

export const appRoute = { 
   home: '/',
   login: '/login',
   terms: '/terms',
   about: '/about' 
};

export const apiProduct = { 
   products: '/products',
   productId: (id) => `/products/${id}`,
};

在常量和配置之间,正确的命名是什么?它们之间有什么区别?

英文:

I'm new to React.js so I'm a bit confused about the difference between constants and configs file in structure of project. As far as I know, constants contain values ​​that cannot be changed and reused in the project, example:

// constants.js

const APPLE = 'APPLE'; 
const BANANA = 'BANANA';

But configs file also do the same thing with constants, example:

// configs.js 

export const appRoute = { 
   home: '/', 
   login: '/login', 
   terms: '/terms', 
   about: '/about' 
};

export const apiProduct = { 
   products: '/products', 
   productId: (id) => /products/${id}, 
};

what is the correct naming between constants and configs? And what is the difference here?

答案1

得分: 1

React在项目结构或文件结构方面通常不会有太多的意见。大多数自定义配置文件都不必遵循任何命名约定,也不必放在特定文件夹中,除非您想要操纵建立在React之上的某个特定框架的行为。

就我个人而言,我从未使用像您演示的大写字符串常量,因为对于大多数用例来说,如果您需要在整个项目中共享固定值,配置对象更方便。

从技术上讲,您的 constants.jsconfigs.js 之间几乎没有什么区别。您在 constants.js 中的 const 变量将来不会被重新赋值,但 configs 中的对象属性可以被重新赋值。

英文:

React in general is not very opiniated regarding project structure or file structure. Mostly any custom config files you create neither have to follow any naming conventions nor need to be placed in some particular folder unless you want to manipulate the behavior of some specific framework that builts on top of React.

Personally, I never use classic string constants spelled all uppercase like you demonstrated since for most use cases config objects are more handy if you need to share fixed values across your whole project.

Technally speaking there is very little difference between your constants.js and configs.js. Your const variables in constants.js cannot be reassigned in the future however an object's property in configs can.

答案2

得分: 0

我认为这个问题需要更新,但如果你创建一个单独的文件来存放一个常量变量,我们可以通过导入来轻松访问该变量及其值,而这些值将不会被重新分配。重复使用这些值是有利的。
你可能会发现这个链接对理解这个概念很有用。

https://www.w3schools.com/js/js_modules.asp

谢谢

英文:

I think the question needs to be updated, but if you create a separate file with for a constant variable, we can easily access that variable and its values by using imports and those values will not be reassigned. It is advantageous to reuse those values.
You might find this link useful for understanding the concept.

https://www.w3schools.com/js/js_modules.asp

Thanks

huangapple
  • 本文由 发表于 2023年7月24日 15:04:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定