codestyle

5629 단어 style
만약 당신의 코드가 읽기 쉽다면, 코드 중의 버그도 매우 적을 것이다. 왜냐하면 일부 버그는 디버깅을 용납할 수 있고, 다른 개발자들이 당신의 프로젝트에 참여할 때의 문턱도 비교적 낮기 때문이다.따라서 프로젝트에 많은 사람이 참여한다면 공통된 코드 스타일 약속을 취하는 것이 필요하다.
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

    좋은 웹페이지 즐겨찾기