標籤:javascript
多行注釋使用/**……*/,需要包含一個描述,所有參數的具體類型的值還有返回值。
// bad// make() returns a new element// based on the passed in tag name//// @param {String} tag// @return {Element} elementfunction make(tag) {// ...stuff...return element;}// good/*** make() returns a new element* based on the passed in tag name** @param {String} tag* @return {Element} element*/function make(tag) {// ...stuff...return element;}
單行注釋使用//,把單行注釋放在語句的上一行,並且在注釋之前空一行。
// badvar active = true; // is current tab// good// is current tabvar active = true;// badfunction getType() {console.log('fetching type...');// set the default type to 'no type'var type = this._type || 'no type';return type;}// good. God Bless!function getType() {console.log('fetching type...');// set the default type to 'no type'var type = this._type || 'no type';return type;}
如果你指出的問題需要重新置放或者提出一個待解決的問題需要實現,給注釋添加FIXME or TODO 首碼有利於其他開發人員快速理解。這些注釋不同於通常的注釋,因為它們是可實施的。這些實施措施就是FIXME -- need to figure this out or TODO -- need to implement.
function AAA() {// <strong>使用// FIXME:給一個問題作注釋 </strong>FIXME: shouldn't use a global heretotal = 0;return this;}
function BBB() {// <strong>使用//TODO:給問題解決方案作注釋 </strong>TODO: total should be configurable by an options paramthis.total = 0;return this;}
God bless You!
【學習筆記】JavaScript編碼規範- 注釋