Skip to content

bitzip/front-end-convention

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 

Repository files navigation

Front end convention

HTML

  1. HTML content type
  2. HTML标签语义
  3. 代码缩进
  4. 页内CSS位置
  5. 页内Javascript位置

CSS

  1. 盒子模型
  2. 类命名
  3. 样式分行
  4. 样式位置
  5. stylus嵌套结构

Javacript

  1. 分号
  2. 引号
  3. 判断和比较
  4. 变量命名约定
  5. 变量和函数位置
  6. 读取属性

HTML content type

  • 使用HTML5文件类型

    <!DOCTYPE HTML>
    <html>
    <head>
      <meta charset="UTF-8">
      <title></title>
    </head>
    <body>
      
    </body>
    </html>

HTML标签语义

  • 使用语义HTML标签

    <!-- bad -->
    <div class="nav">
      <p><a>page1</a></p>
      <p><a>page2</a></p>
    </div>
    
    <!-- good -->
    <ul class="nav">
      <li><a>page1</a></li>
      <li><a>page2</a></li>
    </ul>
    
    
    <!-- bad -->
    <span style="cursor:pointer">点击打开<span>
    
    <!-- good -->
    <a href="javascript:;">点击打开</a>
    
    
    <!-- bad -->
    <!-- divist -->
    <div class="shipping-wrapper">
      <div class="name"></div>
      <div class="meta">
        <div class="address"></div>
      </div>
      <div class="desc"></div>
    </div>
    
    <!-- good -->
    <section class="wrapper">
      <cite></cite>
      <span class="meta">
        <address></address>
      </span>
      <div class="desc"></div>
    </section>

代码缩进

  • 使用两个空格(适用HTML、CSS、Javacript)

页内CSS位置

  • 放在<head>标签内

    /* bad */
    <div>
      <style>
        .foo {
          color: #000;
        }
      </yle>
    </div>
    
    /* good */
    <head>
      <style>
        .foo {
          color: #000;
        }
      </style>
    </head>

页内Javascript位置

  • <script> 根级代码不缩进

    /* bad */
    <section>
      <div>
        <h1>Foobar</h1>
        <script>
          $('h1').html('gotcha.')
        </script>
      </div>
    </section>
    
    /* good */
    <section>
      <div>
        <h1>Foobar</h1>
    <script>
    $('h1').html('gotcha.')
    </script>
      </div>
    </section>

盒子模型

  • 使用content-box作为开发默认值

    /* bad */
    box-sizing: content-box;
    
    /* good */
    * {
      box-sizing: border-box;
    }

类命名

  • 类名使用-连接符

    /* bad */
    .btn_save {
      background: cyan
    }
    
    /* good */
    .btn-save {
      background: cyan
    }

样式分行

  • 样式属性一个一行

    /* bad */
    .body { background: #ddd; color: #333 }
    
    /* good */
    .body {
      background: #ddd;
      color: #333 
    }

样式位置

  • 每个样式都有各自的作用域

    /* bad */
    将有样式都写在main.css文件内
    
    /* good */
    如果样式不是通用样式,将样式写在页面的<head>标签内

Stylus样式结构

  • 自身样式在前,嵌套样式在后

    /* bad */
    .navigation
      .h2
         color: #333
      width: 300px
      display: none
    
    /* good */
    .navigation
      width: 300px
      display: none
    
      .h2
        color: #333

Javacript

分号

  • 我们不使用分号。除了行首是"[,(,+,-,/",在前面添加分号

    // bad
    var dom = document.getElementById('foo');
    dom.style.display = 'none';
    
    // good
    var dom = document.getElementById('foo')
    dom.style.display = 'none'

引号

  • 尽量使用单引号

    // bad
    var pizza = "yummy"
    var hamburg = "'yummy'?"
    
    // good
    var pizza = 'yummy'
    var hamburg = '"yummy"?'

判断和比较

  • 使用严格检查 ===, !== 而不是 ==, !=

    // bad
    if (name !== '') {
      // ...stuff...
    }
    
    // good
    if (name) {
      // ...stuff...
    }
    
    // bad
    if (collection.length > 0) {
      // ...stuff...
    }
    
    // good
    if (collection.length) {
      // ...stuff...
    }

命名约定

  • 避免使用单个字母作为变量,尽可能使用有意义的变量名。

    // bad
    function q() {
      // ...stuff...
    }
    
    // good
    function query() {
      // ..stuff..
    }
  • 使用驼峰命名法

    // bad
    var this_is_my_object = {}
    function c() {}
      var u = new user({
      name: 'Bob Parr'
    })
    
    // good
    var thisIsMyObject = {}
    function thisIsMyFunction() {}
      var user = new User({
      name: 'Bob Parr'
    })
  • 类名使用帕斯卡命名法

    // bad
    function user(options) {
      this.name = options.name
    }
                              
    var bad = new user({
      name: 'nope'
    })
                              
    // good
    function User(options) {
      this.name = options.name
    }
                              
    var good = new User({
      name: 'yup'
    })

变量和函数位置

  • 变量->函数->调用

    // bad
    
    var name = 'foo'
    greet(name)
    
    function greet(name) {
      alert(name)
    }
    
    // good
    var name = 'foo'
    function greet(name) {
      alert(name)
    }
    greet(name)

读取属性

  • 使用点语法读取属性

    var person = {
      name: 'Kelly'
    }
    
    // bad
    person['name']
    
    // good
    person.name

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published