Considering I am going to work with a larger team in the future, I'm trying to teach myself some basic annotation and documentation principles for front-end languages. Currently I'm working on JS.
For the most part, I'm using Google's Style Guide as a go-to, but I still have some questions.
Let's say I have an ajax function like so:
function initFunction(src, wrapper) {
$.getJSON(src, {
format: "json"
}).done(function(data) {
var wrapper = $(wrapper),
contents = callAnotherFunction($(data)[0]);
// Populates the wrapper element.
wrapper.append(contents );
}).fail(function(jqXHR, textStatus, errorThrown) {
alert(textStatus + ": " + errorThrown);
});
}
The function has two @param
, src and wrapper. Here are some questions.
callAnotherFunction(), then, takes as an argument an Object and it should return some HTML.
{Object}
?"#myId"
, String?String
?/**
* This is a description of this function. It gets a JSON file, uses it as
* a jQuery object, and then call another function with the new data.
* @param {Object} src JSON file to parse.
* @param {String} wrapper HTML element to use as a wrapper for the output.
* @return {Null}
*/
The standards for JavaScript are the ECMAScript Language Specification (ECMA-262) and the ECMAScript Internationalization API specification (ECMA-402).
There are two types of variables in JavaScript : local variable and global variable.
These days, most cross-browser JavaScript problems are seen: When poor-quality browser-sniffing code, feature-detection code, and vendor prefix usage block browsers from running code they could otherwise use just fine. When developers make use of new/nascent JavaScript features, modern Web APIs, etc.)
src
is a string containing the url (it doesn't matter retrieving the url retrieves JSON) so the type is string. More info here.Type: htmlString or Element or Array or jQuery
DOM element, array of elements, HTML string, or jQuery object to insert at the end of each element in the set of matched elements.
So it depends on what you want to document your function as accepting. If you want to document it as accepting multiple types, you use parentheses and the |
character (example below).
Close, but you don't want a return type. Some people also put a blank line between the description and the params, but that's not required by the parser.
/**
* This is a description of this function. It gets a JSON file, uses it as
* a jQuery object, and then call another function with the new data.
*
* @param {Object} src JSON file to parse.
* @param {(String|jQuery|DOMElement)} wrapper HTML element to use as a wrapper for the output.
*/
function initFunction(src, wrapper) {
// ...
In the above, we've indicated that wrapper
can be a string, jQuery object, or DOMElement. We didn't get into the details of it possibly being an array, nor whether the string is a selector or an HTML fragment. The description would need to handle that. With a lot of choices, you may have to fall back on {*}
.
If the parser may not be able to tell if this is a function, you'd also add the @function
tag:
/**
* This is a description of this function. It gets a JSON file, uses it as
* a jQuery object, and then call another function with the new data.
*
* @function
*
* @param {Object} src JSON file to parse.
* @param {(String|jQuery|DOMElement)} wrapper HTML element to use as a wrapper for the output.
*/
var initFunction = function(src, wrapper) {
// ...
Depending on context, you may prefer @method
to @function
(they're synonyms).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With