关于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总的的边数减去重叠的 ...
随机推荐
- linux下各种代理的设置
http://los-vmm.sc.intel.com/wiki/OpenStack_New_Hire_Guide#Apply_JIRA_account Set up your proxy. The ...
- 菜鸟级SQL Server21天自学通(文档+视频)
SQL语言的主要功能就是同各种数据库建立联系,进行沟通.按照ANSI(美国国家标准协会)的规定,SQL被作为关系型数据库管理系统的标准语言.SQL语句可以用来执行各种各样的操作,例如更新数据库中的数据 ...
- golang win32编程的一个dll坑
例子 package main import ( "github.com/lxn/win" "strconv" "syscall" ) fu ...
- HDU 4937 Lucky Number 规律题_(:зゝ∠)_
把全部合法的进制打出来会发现合法的进制都是在 n/3 n/4 n/5的边上 然后暴力边上的进制数.. #include <cstdio> #include <set> type ...
- C# OR/Mapping 数据处理模式学习
为什么要提出O/R Mapping概念 程序语言已经由面向过程的模型全面转向为面向对象的模型,UML的出现更加革新了软件开发方法论.然而数据库模型却从未随着开发语言的进步而随之革新,仍然使用面向关系的 ...
- Table显示边框
style="border-bottom: 1px solid #CCCCCC;"
- 重构HTML改善web应用设计
本文从良构,有效性,布局三个角度,结合往日项目开发经历, 整理总结重构HTML改善Web应用设计的几点规则和做法.部分参考自<重构HTML改善Web应用设计>. 重构.什么是重构?为什么要 ...
- hive 分配map数过少导致任务执行慢
数据表大概150M,但是只有几个字段,导致行数特别多,当使用正则表达式去匹配时执行较慢. 解决思路:增大map数; //设置reduce数为150,将原表分成150份,map数无法直接设置,因为和输入 ...
- 如何调试框架中的app
1,在编写的app中添加断点,并重新生成或编译 2,找到框架app的相应位置代开文件把所用到的dll重新替换成上步生成的dll(bin->debug) 3,运行框架,在VS打开调试->附加 ...
- Unity5UGUI 官方教程学习笔记(二)Rect Transform
Rect Transform Posx Posy Posz : ui相对于父级的位置 Anchors :锚点 定义了与父体之间的位置关系 一个锚点由四个锚组成 四个锚分别代表了 ...