关于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总的的边数减去重叠的 ...
随机推荐
- C函数数组元素初始化
初始化时,可随意指定初始化的元素或者元素的范围. 附gnu c 手册. http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html 代码: t ...
- centos6.5 openvpn安装配置
http://m.jb51.net/?host=www.jb51.net&src=http%3A%2F%2Fwww.jb51.net%2Fsoftjc%2F150885.html
- Java实现串口通信的小样例
用Java实现串口通信(windows系统下),须要用到sun提供的串口包 javacomm20-win32.zip.当中要用到三个文件,配置例如以下: 1.comm.jar放置到 JAVA_HOME ...
- CSS中的 REM PX EM
px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的 em是相对长度单位.相对于当前对象内文本的字体尺寸.如当前对行内文本的字体尺寸未被人为设置,则相对于浏览器的默认字体尺寸. ...
- <转>LINQ To SQL 语法及实例大全
一篇很全很强大的linq to sql 总结 来源:http://blog.csdn.net/pan_junbiao/article/details/7015633 目录(?)[-] LINQ to ...
- SQL 2008升级SQL 2008 R2完全教程或者10.00.4000升级10.50.1600
今天将由于需要就将我的SQL 2008升级到SQL 2008 R2. 说到为什么要升级是因为,从另一台机器上备份了一个数据库,到我的机器上还原的时候提示“System.Data.SqlClient.S ...
- 获取 web容器中的bean
public class WebContextBeanFinder { public static Object getBean(String beanId) { ServletContext ser ...
- MQ学习(一)----JMS规范(转发整合)
最近进行ActiveMQ的学习,总结下已被不时之需. JMS规范: JMS即Java消息服务(Java Message Service)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的A ...
- C++链接库
静态链接库在程序编译链接过程中就导入lib文件并且包含在生成的exe文件里,而动态链接库DLL是在程序运行中由程序加载和卸载的,也就是说它是动态的,当然动态链接库DLL也可以静态加载当做静态来用: 静 ...
- 使用VisualStudio发布ASP.NET网站
1.右击网站点击“发布网站” 2.选择或导入发布配置文件.→新建配置文件. 3.输入名称test.→点击确定. 4.发布方法选择文件系统. 5.选择目标位置.→点击下一步 6.文件发布选项选择三个选项 ...