JavaScript Patterns 2.12 Writing API Docs

Free and open source tools for doc generation:

the JSDoc Toolkit (http://code.google.com/p/jsdoc-toolkit/)

and YUIDoc (http://yuilibrary.com/projects/yuidoc).

Process of generating API documentation

? Writing specially formatted code
blocks

? Running a tool to parse the code and the comments

? Publishing the results of the tool, which are most often HTML
pages


/**

* Reverse a string

*

* @param {String} input String to reverse

* @return {String} The reversed string

*/

var reverse = function (input) {
// ...
return output;
}; 

The contents of app.js
starts like this:


/**

* My JavaScript application

*

* @module myapp

*/

Then you define a blank object to use as a namespace:

var MYAPP = {};

And then you define an object math_stuff that has two methods: sum() and
multi():


/**

* A math utility

* @namespace MYAPP

* @class math_stuff

*/

MYAPP.math_stuff = {

/**

* Sums two numbers

*

* @method sum

* @param {Number} a First number

* @param {Number} b The second number

* @return {Number} The sum of the two inputs

*/

sum: function (a, b) {
return a + b;
},

/**

* Multiplies two numbers

*

* @method multi

* @param {Number} a First number

* @param {Number} b The second number

* @return {Number} The two inputs multiplied

*/
multi: function (a, b) {
return a * b;
}
}; 

@namespace

The global reference that contains your object.

@class

A misnomer (no classes in JavaScript) that is used to mean either an object
or a constructor function.

@method

Defines a method in an object and specifies the method name.

@param

Lists the arguments that a function takes. The types of the parameters are in
curly braces, followed by the parameter name and its description.

@return

Like @param, only it describes the value returned by the method and has no
name.

? @constructor hints that this “class” is actually a constructor function

? @property and @type describe properties of an object


/**

* Constructs Person objects

* @class Person

* @constructor

* @namespace MYAPP

* @param {String} first First name

* @param {String} last Last name

*/

MYAPP.Person = function (first, last) {

/**

* Name of the person

* @property first_name

* @type String

*/
this.first_name = first;

/**

* Last (family) name of the person

* @property last_name

* @type String

*/
this.last_name = last;
};

/**

* Returns the name of the person object

*

* @method getName

* @return {String} The name of the person

*/

MYAPP.Person.prototype.getName = function () {
return this.first_name + ‘ ‘ + this.last_name;
};

YUIDoc Example

JavaScript Patterns 2.12 Writing API Docs,布布扣,bubuko.com

时间: 2024-10-16 15:22:32

JavaScript Patterns 2.12 Writing API Docs的相关文章

JavaScript Patterns 2 Essentials - Writing Maintainable Code

Revisiting the code after some time has passed requires: ? Time to relearn and understand the problem ? Time to understand the code that is supposed to solve the problem As the application matures, many other things happen that require your code to b

JavaScript Patterns 2.11 Writing Comments

Document all functions, their arguments and return values, and also any interesting or unusual algorithm or technique. Think of the comments as hints to the future readers of the code; the readers need to understand what your code does without readin

JavaScript Patterns 5.4 Module Pattern

MYAPP.namespace('MYAPP.utilities.array'); MYAPP.utilities.array = (function () { // dependencies var uobj = MYAPP.utilities.object, ulang = MYAPP.utilities.lang, // private properties array_string = "[object Array]", ops = Object.prototype.toStr

JavaScript Patterns 3.7 Primitive Wrappers

Primitive value types: number, string, boolean, null, and undefined. // a primitive number var n = 100; console.log(typeof n); // "number" // a Number object var nobj = new Number(100); console.log(typeof nobj); // "object"  One reason

JavaScript Patterns 7.1 Singleton

7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. This means that the second time you use the same class to create a new object, you should get the same object that was created the first time. var obj =

JavaScript Patterns 2.2 - Minimizing Globals

Access a global variable in a browser environment: myglobal = "hello"; // antipattern console.log(myglobal); // "hello" console.log(window.myglobal); // "hello" console.log(window["myglobal"]); // "hello"

JavaScript Patterns 4.7 Init-Time Branching

When you know that a certain condition will not change throughout the life of the program, it makes sense to test the condition only once. Browser sniffing (or feature detection) is a typical example. // BEFORE var utils = { addListener : function(el

JavaScript Patterns 3.6 Regular Expression Literal

1. Using the new RegExp() constructor // constructor var re = new RegExp("\\\\", "gm"); 2. Using the regular expression literal // regular expression literal var re = /\\/gm;  when using the RegExp()constructor, you also need to escape

JavaScript Patterns 2.10 Naming Conventions

1. Capitalizing Constructors var adam = new Person(); 2. Separating Words camel case - type the words in lowercase, only capitalizing the first letter in each word. upper camel case, as in  MyConstructor(), lower  camel  case,  as  in  myFunction(),