Code conventions and best practices for javascript
Apr 11, 2014 · 2 分钟阅读

  • JavaScript programs should be stored in and delivered as .js files

  • script tags should be placed as late in the body as possible, There is no need to use the language or type attributes

  • The unit of indentation is four spaces

  • Avoid long lines

  • Be generous with comments

  • It is preferred that each variable be given its own line and comment, They should be listed in alphabetical order if possible, variables that are only declared but do not get a value assigned may be declared on the same line

  • When a function is to be invoked immediately, the entire invocation expression should be wrapped in parens

  • The with statement should not be used

  • Use the === and !== operators

  • eval is Evil

  • Do not pass strings to setTimeout or setInterval

  • Function Declarations Within Blocks: No

  • Always preferred over non-standards features(e.g. string.charAt(3) over string[3])

  • In case a variable contains a jQuery object, the variable can start with $

  • Variables should not be defined in the global scope

  • Use the module pattern to encapsulate

  • Namespace your JavaScript if you need to refer to it elsewhere

  • When optimizating, focus on the big things

    • Excessive DOM changes that force the page to re-render

    • Events that get fired all the time (for example, resizing/scrolling)

    • Lots of HTTP requests (and even this is becoming less important)

  • Lazy load assets that aren't immediately required

  • Causing excessive document reflows(DOM modification is slow)

  • Going overboard with file concatenation

see more: JavaScript Style Guide, Best PracticesCode Conventions

← Previous Post Next Post →

Ryun的博客
与其感慨路难行,不如马上出发。

atom css design git html javascript jekyll laravel life mac mobile optimization sublime tool vscode vue
mac

About

你好,❤朋友

这里是 Ryun 的博客 📝

📝记录了我学习 🔎的过程

作为一名前端攻城狮 🦁

希望能和大家一起 🔎

共同进步 🏃

🦁的特长:

  • HTML5
  • CSS3
  • JavaScript
  • Vue.js
  • 微信小程序
  • 熟练配合 Node.js、PHP 和 Laravel
  • 熟练使用各种开发和设计工具

🦁喜欢 🆒🆒的东西

对提升效率和美感的事物有兴趣 😋

欢迎 👏交流


see this hugo-theme-dream-ink