简体   繁体   中英

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.

//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

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)

//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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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