zoukankan      html  css  js  c++  java
  • 关于codestyle

    如果你的代码易于阅读,那么代码中bug也将会很少,因为一些bug可以很容被调试,并且,其他开发者参与你项目时的门槛也会比较低。因此,如果项目中有多人参与,采取一个有共识的编码风格约定非常有必要。

    以todomvc的编码要求为例:

    General Rules

    • Tab indentation
    • Single-quotes
    • Semicolon
    • Strict mode
    • No trailing whitespace
    • Variables at the top of the scope
    • Multiple variable statements
    • Space after keywords and between arguments and operators
    • Return early
    • JSHint valid
    • Consistency

    Example:

    'use strict';
    
    function foo(bar, fum) {
        var i, l, ret;
        var hello = 'Hello';
    
        if (!bar) {
            return;
        }
    
        for (i = 0, l = bar.length; i < l; i++) {
            if (bar[i] === hello) {
                ret += fum(bar[i]);
            }
        }
    
        return ret;
    }

    Read idiomatic.js for general JavaScript code style best practices.

    Anonymous Functions

    When using anonymous functions, leave a space between the function name and opening parenthesis.

    Example:

    (function () {
        'use strict';
    
        var thanks = 'mate';
    })

    Strict mode

    Strict mode should be used wherever possible, but must never be globally applied. Instead, use it inside an IIFE as shown above

    Comments

    Inline comments are a great way of giving new users a better understanding of what you're doing and why.

    It's also helpful to let your functions breathe, by leaving additional lines between statements.

    Example:

    // Ok.
    var removeTodo = function (todoItem) {
        var todoModel = todoItem.getModel(); // Grab the model from the todoItem.
        todoItem.find('.destroy').click(); // Trigger a click to remove the element from the <ul>.
        todoModel.remove(); // Removes the todo model from localStorage.
    };
    
    // Better.
    var removeTodo = function (todoItem) {
        // Grab the model from the todoItem.
        var todoModel = todoItem.getModel();
    
        // Trigger a click to remove the element from the <ul>.
        todoItem.find('.destroy').click();
    
        // Removes the todo model from localStorage.
        todoModel.remove();
    };

    RequireJS

    When using RequireJS, please format your code to these specifications:

    define('Block', [
        'jQuery',
        'Handlebars'
    ], function ($, Handlebars) {
        'use strict';
    
        // Code here.
    });

    JSHint

    When you submit your pull request, one of the first things we will do is run JSHint against your code.

    You can help speed the process by running it yourself:

    jshint path/to/your/app/js

    Your JSHint code blocks must follow this style:

    /*global define, App */
    /*jshint unused:false */

     

    一些达成共识的JavaScript编码风格约定

    http://www.iteye.com/news/28028-JavaScript-code-style-guide

  • 相关阅读:
    从零开始,SpreadJS新人学习笔记【第4周】
    如何使用JavaScript实现前端导入和导出excel文件
    【案例分享】在 React 框架中使用 SpreadJS 纯前端表格控件
    中国高考志愿填报与职业趋势分析
    Vue 2019开发者图谱
    从零开始,SpreadJS新人学习笔记【第3周】
    从零开始,SpreadJS 新人学习笔记(第二周)
    从零开始,SpreadJS 新人学习笔记
    Spread.NET 表格控件 V12.1 正式发布
    汇编语言-端口
  • 原文地址:https://www.cnblogs.com/alexandra/p/3684368.html
Copyright © 2011-2022 走看看