selfexplanatory v0.0.1
selfexplanatory.js
Make your functions and methods self-explanatory with this simple wrapper.
Bring documentation right where it's needed
Whatever you're creating, a library or an API or a widget - if you want someone to use it, let them do so without even looking for the documentation. You know they prefer it that way.
Works in the browser and in nodejs. (npm install selfexplanatory)
What it does
Whenever someone sees the explained function in developer tools, on an object listing etc. it will show the instructions instead of bits of its definition.
If the function gets called incorrectly, It will throw an error containing complete usage information. No need to browse documentation anymore!
Let's explain how it works
explain( <object:explanation>, <function: yourmethod> )
// returns: function with some special behaviourA more complete example maybe:
myMethod : explain({
args: "<string: url>,<function: success callback>",
description: "does stuff",
returns: "string: result",
validator: function (url) {
return (typeof url === "string");
}
}, function (url, callback) { ... });The explanation object consists of the following:
argsrequired a single line describing arguments of the functiondescriptionrequired any description of the function you want to givereturnsoptional just say what gets returnedvalidatoroptional a function that will get called before the actualexplained method with the same arguments. If the function returns something falsy, an error is thrown with the whole explanation in it. Ifvalidatorreturns truthy, your method gets called.
explained method will correctly resolve this reference.
When an invalid call is made, this is thrown:
Usage: function( <string: url>,<function: success callback> )
does stuff
returns: string: resultI don't recommend explaining constructor functions and prototypes, because you loose the instanceof feature.
12 years ago