JS-Aspect Demo 2

Using a double aspect apply technique.

by Ben Clayton

JavaScript

!function(host){function joinPointName(joinPointId){return joinPointId.toLowerCase().replace(/_[a-z]/,function(match){return match[1].toUpperCase()})}function injectAdvice(target,methodName,advice,joinPoint){isFunction(target[methodName])&&(jsAspect.JOIN_POINT.AROUND===joinPoint&&(advice=wrapAroundAdvice(advice)),target[methodName][adviceEnhancedFlagName]||(enhanceWithAdvices(target,methodName),target[methodName][adviceEnhancedFlagName]=!0),target[methodName][joinPoint].unshift(advice))}function wrapAroundAdvice(advice){var wrappedAdvice=function(executionContext,leftAroundAdvices){var oThis=this,nextWrappedAdvice=leftAroundAdvices.shift(),args=toArray(arguments).slice(2);if(!executionContext.isStopped){if(nextWrappedAdvice){var nextUnwrappedAdvice=function(){var argsForWrapped=toArray(arguments);return argsForWrapped.unshift(leftAroundAdvices),argsForWrapped.unshift(executionContext),nextWrappedAdvice.apply(oThis,argsForWrapped)};args.unshift(nextUnwrappedAdvice)}return advice.originalMethodFlagName||args.unshift(executionContext),advice.apply(this,args)}};return wrappedAdvice.__originalAdvice=advice,wrappedAdvice}function enhanceWithAdvices(target,methodName){var originalMethod=target[methodName];originalMethod.originalMethodFlagName=!0,target[methodName]=function(){var returnValue,self=this,method=target[methodName],args=toArray(arguments),executionContext=new ExecutionContext(target,methodName,args);applyBeforeAdvices(self,method,args,executionContext);try{returnValue=applyAroundAdvices(self,method,args,executionContext)}catch(exception){throw applyAfterThrowingAdvices(self,method,exception,executionContext),exception}return applyAfterAdvices(self,method,args,executionContext),applyAfterReturningAdvices(self,method,returnValue,executionContext)};for(var join_point in jsAspect.JOIN_POINT)target[methodName][jsAspect.JOIN_POINT[join_point]]=[];target[methodName][jsAspect.JOIN_POINT.AROUND].unshift(wrapAroundAdvice(originalMethod))}function...