JavaScript for decent coding

Jiedao jdon 2021-05-03 23:29:26
javascript decent coding

Avoid relying on null The difference between and undefined. This often leads to code vulnerability . Use non mandatory equality when checking the equality of any one of them (==)null. Avoid using both to represent different types of “ No value ” situation , for example “unknown field” And “no value for today”.

Be careful not to use true and false check zero . If there are numbers , It usually needs to be treated in the same way as any other number - Unlike other false values .

Floating point operation , There are common pitfalls . Consider doing and doing as little as possible on the client side / Or use JavaScript Of Digital Library .

Pay attention to the function support in different browsers . Or use only those supported by all browsers supported by the application , Or perform incremental enhancement . see also I can use [url=]MDN Document page [/url] Reference on , Browser compatibility table and ECMAScript Compatibility table . Use linter plug-in unit Automatically perform compatibility checks . Some features may be transformed by your build settings , Or is it polyfilled - Make them available in older browsers .

avoid setTimeout() Or something like vulnerability or incomprehension . Through careful selection ( Usually by trial and error ) Run a piece of code after a time delay , It may happen, it may be difficult to achieve / The plan of repair . The reason why this method works ( At least for some time ) Little known to the author , And lead to fragile and unpredictable behavior - Especially when the delay is nonzero .

Pay attention to instance methods that mutate in situations that may not be expected . These include Array On the spot reverse(), And many Moment.js Method ( Seriously consider alternatives , Such as date-fns or Luxon).

Array sort() Convert elements to strings , And sort them alphabetically by default . For anything but strings , It's not likely to be what you want - The function of comparator must be given .

character string replace() Replace the first match only when using a string as a pattern . Global substitution requires regular expressions .

Try to avoid duck Type checking . If the duck To change , They break easily , And it doesn't convey the intention very well .

Avoid layout bumps caused by cross read and write . Group read and write as much as possible Lead to layout / The properties of rearrangement .

Avoid overwork in response to user input . This includes calculation , Screen updates and requests to the server . Consider removing jitters or limiting keyboard input and certain types of mouse events .

Avoid using outdated technology . The progress of language continues to bring about the renewal of many traditional technologies , A better alternative . Use them . Examples include async / await And template string .


本文为[Jiedao jdon]所创,转载请带上原文链接,感谢

  1. 21. Object oriented foundation "problems and solutions of object traversal"
  2. Discussion on hot micro front end: Google AdWords is a real micro front end
  3. Usecallback and usememo for real performance optimization
  4. 【前端圭臬】十一:从规范看 JavaScript 执行上下文(下)
  5. [front end standard] 11: Javascript execution context from the perspective of specification (2)
  6. Hexagonal六角形架构ReactJS的实现方式 - Janos Pasztor
  7. Transaction of spring's reactive / imperative relational database
  8. The implementation of hexagonal hexagonal reactjs Janos pasztor
  9. HTTP状态码:402 Payment Required需要付款 - mozilla
  10. HTTP status code: 402 payment required - Mozilla
  11. Factory mode, constructor mode and prototype mode
  12. Build the scaffold of react project from scratch (Series 1: encapsulating a request method with cache function based on Axios)
  13. Cocos Quick Start Guide
  14. Comparison of three default configurations of webpack5 modes
  15. A case study of the combination of flutter WebView and Vue
  16. CSS: BFC and IFC
  17. A common error report and solution in Vue combat
  18. JS: this point
  19. JS: prototype chain
  20. JavaScript series -- promise, generator, async and await
  21. JS: event flow
  22. Front end performance optimization: rearrangement and redrawing
  23. JS - deep and shallow copy
  24. JavaScript异步编程3——Promise的链式使用
  25. JavaScript asynchronous programming 3 -- chain use of promise
  26. Vue.js组件的使用
  27. The use of vue.js component
  28. How to judge whether a linked list has links
  29. Element UI custom theme configuration
  30. Text image parallax effect HTML + CSS + JS
  31. Spring的nohttp宣言:消灭http://
  32. Vue3 intermediate guide - composition API
  33. Analysis of URL
  34. These 10 widgets that every developer must know
  35. Spring's nohttp Manifesto: eliminate http://
  36. Learn more about JS prototypes
  37. Refer to await to JS to write an await error handling
  38. A short article will directly let you understand what the event loop mechanism is
  39. Vue3 uses mitt for component communication
  40. Characteristics and thinking of ES6 symbol
  41. Two way linked list: I'm no longer one-way driving
  42. Vue event and form processing
  43. Reactive TraderCloud实时外汇开源交易平台
  44. Reactive tradercloud real time foreign exchange open source trading platform
  45. Node.js REST API的10个最佳实践
  46. Ten best practices of node.js rest API
  47. Fiddler advanced usage
  48. Process from Vue template to render
  49. Promise up (asynchronous or synchronous)
  50. Principle and implementation of promise
  51. Vs code plug in sharing - run code
  52. Vue practical notes (1) introduction of Ant Design
  53. Vue actual combat notes (2) introduction of element plus
  54. Introduction to webpack
  55. Webpack construction process
  56. Vue notes
  57. The experience and lessons of moving from ruby megalith architecture to go microservice
  58. Using leancloud to add artitalk module to hexo blog
  59. Implementation of chrome request filtering extension
  60. Detailed introduction of beer import declaration elements and label quarantine [import knowledge]