Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created August 21, 2011 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwaldron/1160879 to your computer and use it in GitHub Desktop.
Save rwaldron/1160879 to your computer and use it in GitHub Desktop.
Based on proposal by Dmitry Soshnikov on es-discuss mailing list
(function( global ) {
var rcomments = /\/\/.+?(?=\n|\r|$)|\/\*[\s\S]+?\*\//g;
function getComments( fn ) {
return ( fn.toString() || "" ).match( rcomments );
};
Object.defineProperty( Function.prototype, "__doc__", {
get: function() {
return getComments( this ).join("\n");
},
writeable: false,
enumerable: true,
configurable: false
});
})( this );
// Example function with inline docs
function fnWithInlineDocs( obj /* an object */ ) {
/*!
* fnWithInlineDocs
*
* @param Object obj some kind of object
*
* @description returns the object param as an object
*
* @return the object
*/
// This is a single line comment, that describes what happens next and why
obj = Object(obj) || {};
// Return the object
return obj;
}
// Test
console.log(
fnWithInlineDocs.__doc__
);
// without .join("\n"), as an array:
[
"/* an object */",
"/*!
* fnWithInlineDocs
*
* @param Object obj some kind of object
*
* @description returns the object param as an object
*
* @return the object
*/",
"// This is a single line comment, that describes what happens next and why",
"// Return the object"
]
// with .join("\n"), as a string:
/* an object */
/*!
* fnWithInlineDocs
*
* @param Object obj some kind of object
*
* @description returns the object param as an object
*
* @return the object
*/
// This is a single line comment, that describes what happens next and why
// Return the object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment