functiongetElementsByClassName(class_name) { var results = []; walkTheDOM(document.body, function (node) { var a; // array of class names var c = node.className; // the node's classname var i; // loop counter // If the node has a class name, then split it into a list of simple names. // If any of them match the requested name, then append the node to the set of results. if (c) { a = c.split(" "); for (i = 0; i < a.length; i += 1) { if (a[i] === class_name) { results.push(node); break; } } } }); return results; }
var x = a ? b : c; // All on one line if it will fit. // Indentation +4 is OK. var y = a ? longButSimpleOperandB : longButSimpleOperandC; // Indenting to the line position of the first operand is also OK. var z = a ? moreComplicatedB : moreComplicatedC;
var userOnline = false; var user = 'nusrat'; var xmlhttp = new XMLHttpRequest(); xmlhttp.open('GET', 'http://chat.google.com/isUserOnline?user=' + user, false); xmlhttp.send(''); // Server returns: // userOnline = true; if (xmlhttp.status == 200) { eval(xmlhttp.responseText); } // userOnline is now true.
其他
不要使用 Function 构造函数。 不要传递字符串给 setTimeout 或者 setInterval。
<!-- TODO(Mark Zhao): remove duplicate tag --> <p><span>2</span></p>
焦点分离
将表现,行为和结构分离:不要在 html 和模板中加入除了结构以外的东西.例如内联样式, center 等标记. 在文档中引入尽可能少的样式和脚本
1 2 3 4 5 6 7 8 9 10
# Bad <h1 style="font-size: 1em;">HTML sucks</h1> <p>I've read about this on a few sites but now I'm sure:<u>HTML is stupid!!1</u><center>I can't believe there's no way to control the styling of my website without doing everything all over again!</center><p>
# Good <h1>My first CSS-only redesign</h1> <p>I've read about this on a few sites but today I'm actually doing it: separating concerns and avoiding anything in the HTML of my website that is presentational. <p>
block,list 或 table 元素
针对每个 block,list 或 table 元素另起一行,并在每个子元素前缩进。这样可读性好
1 2 3 4 5 6
<ul> <li>some list file</li> ... </ul> # ~ <table></table>
/* Modified by zhangyong 1.1->1.2 add update field Java source ………… */ // 2011/8/3 modified by zhangyu start // 2011/8/3 modified by zhangyu end // 注释一般采用" // "," // " 与注释内容间隔一个半角空格的距离。 // 行的后半段使用注释时,使用" // "并以相同的长度进行缩进。 // 为使程序代码易读,用//添加关于处理内容的注释说明。 // 极短的注释可以与它们所要描述的代码位于同一行,但是应该有足够的空白来分开代码和注释。 if (a == 2) { return true; // special case } else { return isPrime(a); // works only for old }
// 动词表: lists/ establish / delete / destroy begin / end first / last get / release get / set increment / decrement put / get lock / unlock open / close min / max old / new start / stop next / previous source / target show / hide send / receive cut / paste up / down
// 系词表: is / has
1 2
function startDatabase() function getDatabaseStatus()
函数名字可以忽略类或对象名称,以避免重复
1 2 3 4 5 6 7 8 9
// good class Font { function getFamily(); }
// bad class Font { function getFontFamily(); }
单例类应该通过一个名为 getInstance()的静态函数返回他们唯一的值
1 2 3 4 5 6
class Toolkit { private static const toolkit = new Toolkit(); public static function getInstance(){ return toolkit; } }
@abstract Documents an abstract class, class variable or method. @access public, private or protected Documents access control for an element. @access private indicates that documentation of element be prevented. @author author name <author@email> Documents the author of the current element. @category Specify a category to organize the documented element’s package into @copyright name date Documents copyright information. @deprecated version Documents a method as deprecated. @example /path/to/example Documents the location of an external saved example file. @exception documents an exception thrown by a method — also see @throws. @global type $globalvarname Documents a global variable or its use in a function or method. @ignore Prevents the documentation of an element @internal private information for advanced developers @link URL @name global variable name Specifies an alias for a variable. For example, $GLOBALS[‘myvariable’] becomes $myvariable @magic phpDocumentor tags}-. @package name of a package Documents a group of related classes and functions. @param type [$varname] description @return type description This tag should not be used for constructors or methods defined with a void return type. @see Documents an association to another method or class. @since version Documents when a method was added to a class. @static Documents a static class or method @staticvar Documents a static variable’s use in a function or class @subpackage @throws Documents an exception thrown by a method. @todo Documents things that need to be done to the code at a later date. @var type a data type for a class variable @version Provides the version number of a class or method.