2012-08-03 26 views
5

只是好奇什么是评论什么参数将传递给回调函数的好方法。评论回调函数

假设我们有以下的代码和不全评论

/** 
* an utterly useless function 
* 
* @param {String} an useless string 
* @param {Boolean} an useless boolean 
* @param {Function} ??? 
*/ 

function Useless (str, bool, callback) { 
    callback(str, bool); 
} 

有什么要说的回调将与海峡和布尔的参数来调用一个好办法吗?

+0

回调函数通过'str'和'bool'?我不确定问题是什么。 – 2012-08-03 00:30:34

+0

问题是如何以一种干净的方式发表评论 – Max 2012-08-03 00:33:26

+0

说这个回调会传递另外两个参数有什么问题? – 2012-08-03 00:34:42

回答

4

通常你只会写函数的调用会讲名字:

/* 
* @param {String} input: the text 
* @param {Function} callback(output, hasChanged): called after calculation 
*/ 

或者,如果参数需要解释,你可以使用一个多说明:

/* 
* @param {String} input: the text 
* @param {Function} callback(result, change) 
      the function that is called after calculation 
      result {String}: the output of the computation 
      change {Bool}: whether a change has occured 
*/ 
2

我不知道这方面的任何约定。我只会用:

@param {Function} Called on success with the response (string) as the first param and the status code (int) as the second 

我知道这是相当详细的,但。

另一种办法是做这样的(类似的jQuery是怎么做的,而不是在我所知道的代码,但其文档中)

@param {Function} onSuccess(response, statusCode) 

下面是一个例子http://api.jquery.com/jQuery.ajax/ 这是当然的不同因为这是一个选项对象,文档的结构与内联文档不同。但看看回调,你会看到相似之处。

为了清楚起见,使用回调(response,statusCode)比回调(string,int)更好。如果你必须选择一个。键入前的含义。

+0

亚这就是我现在正在做的事情,为什么我不高兴 – Max 2012-08-03 00:36:39

+0

更新了它的一个例子,它的灵感来自jQuery文档 – 2012-08-03 00:42:09

+0

thx,会看看并试验! – Max 2012-08-03 00:57:33

0

嗯,有有很多方法可以在javascript中添加注释;这里是我的推荐/最佳实践

使用///* */更好,因为那样你就可以使用后者来取出包含其他注释的整个块。但是,如果您想使用自动文档生成工具,则必须使用类似于javaDoc样式的注释。

这是将与YUI DOC工作的范例(最好的)http://developer.yahoo.com/yui/yuidoc/#start

/** 
* This is a description 
* @namespace My.Namespace 
* @method myMethodName 
* @param {String} str - some string 
* @param {Object} obj - some object 
* @param {requestCallback} callback - The callback that handles the response. 
* @return {bool} some bool 
*/ 
    function SampleFunction (str, obj, callback) { 
     var isTrue = callback(str, obj); // do some process and returns true/false. 
     return isTrue ; 
    } 

其他PARAMS例子: - http://usejsdoc.org/tags-param.html

希望这将帮助你:)