JavaScript function decoration based on Aspect-oriented programming
A simple JavaScript library for decorating functions with common and useful cross-cutting concerns like logging, authorization, caching, etc. It is heavily based on closures and functional aspects of JavaScript.
Note: While the library has been tested in browser, it's not tested on Node.js yet.
logged
: Allows logging function calls along with the passed arguments and the return valuessecured
: Secures a function by ignoring (or breaking) unauthorized callscached
: Stores the function return value to cache (memory) and use it for later callsperformanceLogged
: Measures the runtime duration of the functionsingleton
: Makes the function singleton. All future instances will be the same.safeConstructor
: Protects constructor functions against NOT usingnew
. Any call will be considered as an instantiation.async
: Makes synchronous functions asynchronous to prevent UI blocking
- Make a function loggable:
//define a function
function foo {}
//decorate it
Dcor.logged("foo", function (fname, fargs, res) {
console.log("Log> function " + fname + " called with: ", fargs, " and returned: ", res);
});
//run it
foo(); //args and result value should be logged from now on
logged
can be used to log all functions in the browser for any website. To do that:
- Copy/paste contents of Dcor.js in the browser console
- Decorate all functions like this:
Dcor.logged(".", function (fname, fargs, res) {
console.log("Dcor Log> " + fname + " called with: ", fargs, " and returned: ", res);
});
- Make a synchronous funtion asynchronous:
//define a function
function lengthyOperation() {
//a long running synchrounous task...
};
//decorate it
Dcor.decorate("lengthyOperation", [{ //or simply user Dcor.async
type: "async",
callback: function (fname, fargs, res, f, ns) {
console.log("Async> function " + fname + " called with: ", fargs, " and returned: ", res);
}
}]);
//run it
lengthyOperation(); //it now runs asynchronously and will not block the UI
See more examples