Watch 5 Star 14 Fork 4

散漫的水元素 / aspects-jsJavaScript

Create your Gitee Account
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
javascript面向切面编程,可用于日志输出,事务处理等无关业务的统一代码处理 spread retract

Clone or download
Notice: Creating folder will generate an empty file .keep, because not support in Git




Use aspect in node js


Just install by npm

$ npm install --save aspects-js


You need require aspects-js at first of entry js file


3.Add aspect

Add a js file to write an aspect. First, you should require class Aspect from aspects-js.

//file: testAspect.js
const { Aspect } = require('aspects-js');

Secondly, you should declare a class extends Aspect and implements property pointcut and functions for join point.

//file: testAspect.js
class TestAspect extends Aspect {
    get pointcut() { return '*.do*()' },
    before() { console.log('this is for before join point') },
    after() { console.log('this is for after join point') }

Then, you should exports an instance of your class which is extends Aspect

//file: testAspect.js
module.exports = new TestAspect();

At last, require your aspects at entry.js file

//file: entry.js

Now, all classes when you required will be cut by all your aspects.

4.Classes and Interfaces

Interface Aspect

interface Aspect {
    readonly pointcut: Pointcut | string | ((joinPoint: JoinPoint) => boolean);
    readonly order: number;

    after(joinPoint: JoinPoint, result: any, error: Error);
    afterReturn(joinPoint: JoinPoint, result: any): any;
    afterThrow(joinPoint: JoinPoint, error: Error): void;
    before(joinPoint: JoinPoint):void;
    around(joinPoint: JoinPoint): any;

Class JoinPoint

class JoinPoint {
    readonly type: Class;
    readonly fun: Function;
    readonly thisArg: any;
    readonly target: any;
    readonly args: any[];

    proceed(...args: any[]): any;

Class Pointcut

class Pointcut {
    constructor(pointcut: string);

    matches(joinPoint: JoinPoint): boolean;

5.Pointcut expression



2.Keyword execution


3.Keyword within




4.Operators and wildcards

> * Match all word wildcards


Match all methods which's a name is start with do and in classes which's a name is end with Service

> ? Match one word wildcards


> + Or operate for name


Just match all methods in classes which's a name is Test1 or Test2

> |,|| Or operate for condition


Just match all methods in classes which's a name is Test1 or Test2

> &,&& And operator for condition


Just match method abc in class Test1

> ! Not operate for condition


match all methods except the methods in class Test

> () Brackets operator for condition

Increase the priority of expressions

> () Call operator for function


Match all methods which's a name is abc


Match all methods which's a name is abc and has one argument that instance of class Type1

> , Split operator for arguments


Match all methods which has two arguments that then first one is instance of class Type1 and the second one is instance of class Type2

> . Property operator for search methods of classes


Match the method abc of class Test

> .. Multiple arguments operator for arguments

Match none or multiple arguments


order property of interface Aspects

The Higher precedence when the value is lower

6.Updates and releases


support function for pointcut add property order for class Aspects


use AST for pointcut

Comments ( 0 )

Sign in for post a comment



231008 48f1a665 1899542 231017 9a6720c6 1899542