美文网首页
ENJOY 前端代码规范-初版

ENJOY 前端代码规范-初版

作者: 木木的mt | 来源:发表于2016-08-26 17:17 被阅读0次

    本文档的目标是使 ENJOY 前端小组 JavaScript 代码风格保持一致


    1. 缩进统一使用 4个空格做为一个缩进层级,远离 2个空格 或 tab字符吧基友们
    function hello (name) {
           console.log('hi', name)
    }
    
    1. 单双引号统一纯字符串使用单引号,其他双引号
    console.log('hello there')
    $("<div class='box'>")
    
    1. 关键字后加空格
      关键字有介么多:if / else / for / while / function / switch / do / try / catch / finally
    if (condition) { ... }   // ✓ ok
    if(condition) { ... }    // ✗ avoid 
    
    1. 函数声明、具名函数表达式中,函数名和( 之间加空格
    function name (arg) { ... }   // ✓ ok 
    function name(arg) { ... }    // ✗ avoid
    
    run(function () { ... })      // ✓ ok 
    run(function() { ... })       // ✗ avoid 
    
    1. 二元运算符两侧必须有一个空格,一元运算符与操作对象之间不能有空格。
    // ✓ ok 
    var x = 2
    var message = 'hello, ' + name + '!'
    var a = !arr.length;
    a++;
    
    // ✗ avoid 
    var x=2
    var message = 'hello, '+name+'!'
    var a = ! arr.length;
    a + + ;
    
    1. , 后面要加空格,前别加呀
    // ✓ ok 
    var list = [1, 2, 3, 4]
    function greet (name, options) { ... }
    
    // ✗ avoid 
    var list = [1,2,3,4]
    function greet (name,options) { ... }
    
    1. 语句换行不加;,晓得不啦
    // ✓ ok 
    var list = [1, 2, 3, 4]
    function greet (name, options) { ... }
    
    //✗ avoid 
    var list = [1, 2, 3, 4];
    function greet (name, options) { ... };
    
    1. === 代替 ==
      例外: obj == null ( check null || undefined)
    if (name === 'John')   // ✓ ok 
    if (name == 'John')    // ✗ avoid 
    
    if (name !== 'John')   // ✓ ok 
    if (name != 'John')    // ✗ avoid 
    
    1. 对于 if...else...,在else前不要添加一个换行
    // ✓ ok 
    if (condition) {
      // ... 
    } else {
      // ... 
    }
    
    // ✗ avoid 
    if (condition) {
      // ... 
    }
    else {
      // ... 
    }
    
    1. 在 if...else... 语句中,如果有多行,请使用省略块{...} , 一行的可以不用撒
    // ✓ ok 
    if (options.quiet !== true) console.log('done')
    
    // ✓ ok 
    if (options.quiet !== true) {
      console.log('done')
    }
    
    // ✗ avoid 
    if (options.quiet !== true)
      console.log('done')
    
    1. 报错要处理昂
    // ✓ ok 
    run(function (err) {
      if (err) throw err
      window.alert('done')
    })
    
    // ✗ avoid 
    run(function (err) {
      window.alert('done')
    })
    
    1. 多余的换行是不允许滴
    // ✓ ok 
    var value = 'hello world'
    console.log(value)
    
    // ✗ avoid 
    var value = 'hello world'
    换行
    换行
    换行(打不出来,自行脑补可以伐?
    console.log(value)
    
    1. 对于三目运算如果有多行,?: 请放在语句前面,
    // ✓ ok 
    var location = env.development ? 'localhost' : 'www.api.com'
    
    // ✓ ok 
    var location = env.development
      ? 'localhost'
      : 'www.api.com'
    
    // ✗ avoid 
    var location = env.development ?
      'localhost' :
      'www.api.com'
    
    1. ( , [, ,如果以这三个符号为开头,符号前加上;
    // ✓ ok 
    ;(function () {
          window.alert('ok')
    }())
    // ✓ ok 
    ;[1, 2, 3].forEach(bar)
    // ✓ ok 
    ;`hello`.indexOf('o')
    
    // ✗ avoid 
    (function () {
          window.alert('ok')
    }())
    // ✗ avoid 
    [1, 2, 3].forEach(bar)
    // ✗ avoid 
    `hello`.indexOf('o')
    

    当然,我们一般这么做:

    var nums = [1, 2, 3]
    nums.forEach(bar)
    

    替换

    ;[1, 2, 3].forEach(bar)
    

    更多待补充...

    相关文章

      网友评论

          本文标题:ENJOY 前端代码规范-初版

          本文链接:https://www.haomeiwen.com/subject/lcqzsttx.html