如果你的代码易于阅读,那么代码中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

关于codestyle的更多相关文章

  1. android代码规范和studio配置CodeStyle

    studio配置CodeStyle可以很好的帮助我们检测代码规范性,保持大家的代码统一,来看看怎么配置和使用吧 代码规范,自己公司的一套 代码规范 一.      简介 A.    目的 本文提供一整 ...

  2. lintcode bugfree and good codestyle note

    2016.12.4, 366 http://www.lintcode.com/en/problem/fibonacci/ 一刷使用递归算法,超时.二刷使用九章算术的算法,就是滚动指针的思路,以前写py ...

  3. codestyle 设置问题

    参考: https://blog.csdn.net/hugh77/article/details/43268195 使用 4 空格缩进,而非 TAB. 在小缩进(可以嵌套更深)和大缩进(更易读)之间, ...

  4. my codestyle

    代码风格 缩进 缩进采用4个空格或tab. 原则是:如果地位相等,则不需要缩进:如果属于某一个代码的内部代码就需要缩进. 变量命名 变量命名遵守遵从驼峰命名法,统一使用lowerCamelCase风格 ...

  5. Android Weekly Notes Issue #222

    Android Weekly Issue #222 September 11th, 2016 Android Weekly Issue #222 ARTICLES & TUTORIALS Fo ...

  6. jetbrain系列IDE设置

    1.代码提示默认ctrl+space(这是全角半角切换),改为alt+/,这与cyclic expand word冲突,直接删掉它就可以了 2.ctrl+M,进入presentation mode,与 ...

  7. IntelliJ IDEA 快捷键大全

    IntelliJ IDEA 快捷键大全 (2012-03-27 20:33:44) 转载▼ 标签: ide intellij快捷键 杂谈 分类: IDE工具 最近刚接触IntelliJ这个工具,用了几 ...

  8. AndroidStudio导入Eclipse的代码格式化文件

    对于一个团队来说,使用统一的代码格式是非常重要的,否则在使用版本控制工具时,会出现大量的冲突.在Eclipse里,我们可以通过一些xml来进行代码格式的统一,但是这些文件要应用在AndroidStud ...

  9. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

随机推荐

  1. LDA的一些资料

    LDA-math-汇总 LDA数学八卦 http://www.52nlp.cn/lda-math-%E6%B1%87%E6%80%BB-lda%E6%95%B0%E5%AD%A6%E5%85%AB%E ...

  2. 汉诺塔VII(递推,模拟)

    汉诺塔VII Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  3. Android Intent的几种用法总结【转】

    Intent应该算是Android中特有的东西.你可以在Intent中指定程序要执行的动作(比如:view,edit,dial),以及程序执行到该动作时所需要的资料.都指定好后,只要调用startAc ...

  4. java--jsp+ssh+select动态结合数据和选择(解)

    在三层体系结构和jsp合并项目,如何实现select动态绑定数据和动态选择指定的行?让我们来看看下面的: 1.首先定义一个Bean分类.它用于实例select的结合数据中的每一个id和name: pu ...

  5. ArcMap制图_显示指定区域地图内容

    摘要:有一张完整的中国地图,有时仅要求针对某一特定区域制图,那么如何在不进行裁剪的情况下仅显示该区域范围的要素内容? 步骤: 1.打开ArcMap,加载完整的中国地图: 2.将要显示的区域范围制作成一 ...

  6. C#导出Word或Excel文件总显示Html标记

    原因:Word或Excel文件包含的GridView没有查询到数据.

  7. 解决Metadata file does not match checksum错误

    1.清空缓存执行: # yum clean all 先把就的缓存数据都去掉. 2.下载metadata和校验数据先进入yum对应的目录,再下载: # cd /var/cache/yum/rpmforg ...

  8. VS2010断点无效

    可能的原因如下: 1. 菜单tools->Options->Debugging->General,有个Require  source files to exactly match t ...

  9. centos 安装mysql密码修改后还是不能连接的原因

    centos 上安装mysql密码修改后还是不能连接出现错误:ERROR 1142 (42000): SELECT command denied to user ''@'localhost' for ...

  10. mysql快速翻页查询方法

    SELECT SQL_NO_CACHE *FROM softdb_testWHERE id > (SELECT idFROM softdb_testORDER BY id DESCLIMIT 5 ...