简体   繁体   English

在 javascript 中编写 object 并做出反应的最佳实践是什么

[英]What is best practice for writing object in javascript and react

I have some examples when I write object, It's very hard to select what the best practice(coding style) is.当我写 object 时,我有一些例子,select 很难理解最佳实践(编码风格)是什么。

//destructuring single property
const {foo} = this.state;
const { foo } = this.state;

//destructuring multi properties
const {foo, bar} = this.state;
const { foo, bar } = this.state;

//declaration and initialize single object
const obj = {foo: 'foo'}
const obj = { foo: 'foo' }
const obj = {
    foo: 'foo'
}

ps ps

I know, my question is quite far from the real coding problem.我知道,我的问题与真正的编码问题相去甚远。
but I want to know what the better idea for beautify code.但我想知道美化代码的更好主意。 (I don't use prettier, or beautify because my company don't use these extension, so I can not use for not changing previous code by the extensions) (我不使用prettier,或者beautify,因为我的公司不使用这些扩展,所以我不能用于不通过扩展更改以前的代码)

//destructuring multi properties
const { 
foo, 
bar } = this.state;

//declaration and initialize single object
const obj = {
    foo: 'foo'
}

this will be more readable if you had more than one key and i think prettier will also beautify code like this.如果你有多个键,这将更具可读性,我认为更漂亮也会美化这样的代码。

  • Ask your co-workers what they prefer问问你的同事他们喜欢什么
  • Read some of your own old code - old enough that you don't remember it well - and see how you feel about it.阅读一些你自己的旧代码——老到你记不太清了——看看你对它的感觉。

There is not a "best" answer to this question, and it's probably more of a topic for Reddit than SO.这个问题没有“最佳”答案,对于 Reddit 来说,这可能比 SO 更重要。

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

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