扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:开发者在线 来源:开发者在线 2007年10月8日
关键字:
试用
安装所有必要的组件后,我们就可以尝试使用JSDoc创建文档了。要完成这个任务,我使用列表A中的JavaScript代码。
列表A——样本JavaScript代码
<!-- <![CDATA[
/**
* @fileoverview This file contains the information necessary to add client-side
* keyboard restriction functionality to a web page.
*/
/**
* Accept 'Y' or 'N' and return issue an error message.
* @requires keybEdit
*/
var keybYN = new keybEdit('yn','Valid values are 'Y' or 'N'.');/**
* Accept numeric input and return issue an error message.
* @requires keybEdit
*/
var keybNumeric = new keybEdit('01234567890','Numeric input only.');/**
* Accept numeric input and return issue an error message.
* @requires keybEdit
*/
var keybAlpha = new keybEdit('abcdefghijklmnopqurstuvwxyz','Alpha input only.');/**
* Accept alphabetic input and return issue an error message.
* @requires keybEdit
*/
var keybAlphaNumeric = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890/ -,.=!@#$%^&*()_+'":;','Alpha-numeric input only.');/**
* Accept alphabetic or numeric input and return issue an error message.
* @requires keybEdit
*/
var keybDecimal = new keybEdit('01234567890.','Decimal input only.');/**
* Accept decimal input and return issue an error message.
* @requires keybEdit
*/
var keybDate = new keybEdit('01234567890/','Date input only');;/**
* Accept date input and return issue an error message.
* @requires keybEdit
*/
var keybYNNM = new keybEdit('yn','');/**
* Accept numeric input.
* @requires keybEdit
*/
var keybNumericNM = new keybEdit('01234567890','');/**
* Accept alphabetic input.
* @requires keybEdit
*/
var keybAlphaNM = new keybEdit('abcdefghijklmnopqurstuvwxyz','');/**
* Accept alphabetic or numeric input.
* @requires keybEdit
*/
var keybAlphaNumericNM = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890/ -,.=!@#$%^&*()_+'":;','');/**
* Accept decimal input.
* @requires keybEdit
*/
var keybDecimalNM = new keybEdit('01234567890.','');/**
* Accept date input.
* @requires keybEdit
*/
var keybDateNM = new keybEdit('01234567890/','');;/**
* keybEdit
* @class The purpose of this function is to be a constructor for the keybEdit
* object. keybEdit objects are used by the function editKeyBoard to
* determine which keystrokes are valid for form objects. In addition, if an
* error occurs, they provide the error message.
* @constructor
* @param {string} strValid Valid input characters * @param {string} strMsg Error message*/
function keybEdit(strValid, strMsg) {// Variables
var reWork = new RegExp('[a-z]','gi'); // Regular expression// Properties
if(reWork.test(strValid))
this.valid = strValid.toLowerCase() + strValid.toUpperCase();
else
this.valid = strValid;
if((strMsg == null) || (typeof(strMsg) == 'undefined'))
this.message = '';
else
this.message = strMsg;
/**
* @ignore
*/
this.getValid = keybEditGetValid;
/**
* @ignore
*/
this.getMessage = keybEditGetMessage;
/**
* Return a boolean indicating whether or not a key stroke is valid.
* @method getValid
* @return string-boolean Indicates whether or not a key is valid.
*/
function keybEditGetValid() {return this.valid.toString();
}
/**
* Return an error message associated with a key stroke.
* @method getMessage
* @return string Error message.
*/
function keybEditGetMessage() {return this.message;
}
}
/**
* The purpose of this function is to edit edit keyboard input to determine if
* the keystrokes are valid.
* @requires keybEdit
* @param {Object} evt * @param {Object} objKeyb*/
function editKeyBoard(evt, objKeyb) {var strWork = objKeyb.getValid();
var strMsg = ''; // Error message
var blnValidChar = false; // Valid character flag
var intCode = evt.charCode == undefined ? evt.keyCode : evt.charCode;
// Part 1: Validate input
if(!blnValidChar)
switch(true) {case(intCode == 8):
case(intCode == 9):
case(intCode == 10):
case(intCode == 13):
blnValidChar = true;
break;
default:
for(i=0;i < strWork.length;i++)
if(intCode == strWork.charCodeAt(i)) {blnValidChar = true;
break;
}
break;
}
// Part 2: Build error message
if(!blnValidChar) {if(objKeyb.getMessage().toString().length != 0)
alert('Error: ' + objKeyb.getMessage());evt.stopPropagation = true;
try {evt.preventDefault();
}
catch(e) {window.event.returnValue = false; // Clear invalid character
}
}
}
// ]]> -->
使用上述JavaScript代码作为输入需要再一次进入DOS命令提示符,如图E所示。该图还显示了生成文档所需的命令。图F显示的是文档的部分内容。
图E
生成文档过程
图F
生成的文档
选项和提示
从我所举的不完善的例子中你可能会发现,这个例子仅仅触及JSDoc的一些基本功能。为全面了解JSDoc的所有功能,你需要查看表A,其中列出了一些你可以在JavaScript文档中使用的标签。
表A
标签 |
描述 |
|
@addon |
把一个函数标记为另一个函数的扩张,另一个函数的定义不在源文件中。 |
|
@argument |
用大括号中的自变量类型描述一个自变量。 |
|
@author |
函数/类作者的姓名。 |
|
@base |
如果类是继承得来,定义提供的类名称。 |
|
@class |
用来给一个类提供描述,不能用于构造器的文档中。 |
|
@constructor |
描述一个类的构造器。 |
|
@deprecated |
表示函数/类已被忽略。 |
|
@exception |
描述函数/类产生的一个错误。 |
|
@exec |
|
|
@extends |
表示派生出当前类的另一个类。 |
|
@fileoverview |
表示文档块将用于描述当前文件。这个标签应该放在其它任何标签之前。 |
|
@final |
指出函数/类。 |
|
@ignore |
让JSDoc忽视随后的代码。 |
|
@link |
类似于@link标签,用于连接许多其它页面。 |
|
@member |
定义随后的函数为提供的类名称的一个成员。 |
|
@param |
用大括号中的参数类型描述一个参数。 |
|
@private |
表示函数/类为私有,不应包含在生成的文档中。 |
|
@requires |
表示需要另一个函数/类。 |
|
@return |
描述一个函数的返回值。 |
|
@returns |
描述一个函数的返回值。 |
|
@see |
连接到另一个函数/类。 |
|
@throws |
描述函数/类可能产生的错误。 |
|
@type |
指定函数/成员的返回类型。 |
|
@version |
函数/类的版本号。 |
除上面提供的信息外,在JSDoc.pl命令后增加-h或-help选项,就会显示在生成文档时可以使用的选项列表。图G显示的是使用帮助选项的结果。
图G
-help选项
没有太多借口
虽然由JSDoc生成的文档可能与你的本地标准不一致,但它确实提供了一个起点。假如说,例如本地标准是UML,那么你可能想要使用-format选项试图生成XMI。有了JSDoc,似乎不创建文档的借口也在变少。
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。