Created
August 21, 2011 17:25
-
-
Save rwaldron/1160879 to your computer and use it in GitHub Desktop.
Based on proposal by Dmitry Soshnikov on es-discuss mailing list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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__ | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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