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 functionfunctionfoo{}//decorate itDcor.logged("foo",function(fname,fargs,res){console.log("Log> function "+fname+" called with: ",fargs," and returned: ",res);});//run itfoo();//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 functionfunctionlengthyOperation(){//a long running synchrounous task...};//decorate itDcor.decorate("lengthyOperation",[{//or simply user Dcor.asynctype: "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 UISee more examples