关于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
关于codestyle的更多相关文章
- android代码规范和studio配置CodeStyle
studio配置CodeStyle可以很好的帮助我们检测代码规范性,保持大家的代码统一,来看看怎么配置和使用吧 代码规范,自己公司的一套 代码规范 一. 简介 A. 目的 本文提供一整 ...
- lintcode bugfree and good codestyle note
2016.12.4, 366 http://www.lintcode.com/en/problem/fibonacci/ 一刷使用递归算法,超时.二刷使用九章算术的算法,就是滚动指针的思路,以前写py ...
- codestyle 设置问题
参考: https://blog.csdn.net/hugh77/article/details/43268195 使用 4 空格缩进,而非 TAB. 在小缩进(可以嵌套更深)和大缩进(更易读)之间, ...
- my codestyle
代码风格 缩进 缩进采用4个空格或tab. 原则是:如果地位相等,则不需要缩进:如果属于某一个代码的内部代码就需要缩进. 变量命名 变量命名遵守遵从驼峰命名法,统一使用lowerCamelCase风格 ...
- Android Weekly Notes Issue #222
Android Weekly Issue #222 September 11th, 2016 Android Weekly Issue #222 ARTICLES & TUTORIALS Fo ...
- jetbrain系列IDE设置
1.代码提示默认ctrl+space(这是全角半角切换),改为alt+/,这与cyclic expand word冲突,直接删掉它就可以了 2.ctrl+M,进入presentation mode,与 ...
- IntelliJ IDEA 快捷键大全
IntelliJ IDEA 快捷键大全 (2012-03-27 20:33:44) 转载▼ 标签: ide intellij快捷键 杂谈 分类: IDE工具 最近刚接触IntelliJ这个工具,用了几 ...
- AndroidStudio导入Eclipse的代码格式化文件
对于一个团队来说,使用统一的代码格式是非常重要的,否则在使用版本控制工具时,会出现大量的冲突.在Eclipse里,我们可以通过一些xml来进行代码格式的统一,但是这些文件要应用在AndroidStud ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
随机推荐
- openStack images概念及维护
更改以创建镜像的属性 glance image-update img-uuid --property architecture=arm --propertyhypervisor_type=qemu C ...
- 30款基本UX工具 - 用户测试与反馈工具
日期:2013-9-5 来源:GBin1.com 在上一篇30款基本UX工具 - 思维流程工具 & 原型工具中,我们提到了10款用于头脑风暴和原型创建的工具,用于帮助我们在用户体验上可以做的 ...
- SurfaceView 和 View 区别
android.view.View 和 android.view.SurfaceView SurfaceView 是从 View 基类中派生出来的显示类,直接子类有 GLSurfaceView和Vid ...
- 自学xml的几个例子
xml是一种被用来传输和存储数据的语言,下面给出一些学习xml过程一些简单的例子.具体xml语法请转:http://www.w3school.com.cn/xml/xml_intro.asp 例子1: ...
- spring mvc 返回json数据的四种方式
一.返回ModelAndView,其中包含map集 /* * 返回ModelAndView类型的结果 * 检查用户名的合法性,如果用户已经存在,返回false,否则返回true(返回json数据,格式 ...
- hosts文件简析
什么是hosts文件 hosts文件是个什么文件呢?Hosts虽然没有后缀名,其实是个纯文本文件,可以用记事本等文本编辑软件打开.Hosts文件主要用于在本地电脑强制解析域名,Hosts文件里包含映射 ...
- UITableViewCell的4种样式
转自http://blog.csdn.net/crazyzhang1990/article/details/12503163 1.UITableViewCellStyleDefault: Defaul ...
- SGU 149. Computer Network( 树形dp )
题目大意:给N个点,求每个点的与其他点距离最大值 很经典的树形dp...很久前就想写来着...看了陈老师的code才会的...mx[x][0], mx[x][1]分别表示x点子树里最长的2个距离, d ...
- javascript小练习—点击将DIV变成红色(通过for循环遍历)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- smarty函数-转载
Smarty常用函数 2009-08-13 14:05:55| 分类: Php |举报 |字号 订阅 1 .include_once语句: 引用文件路径,路径必需正确. eg:include ...