Giter Site home page Giter Site logo

jaspect's Introduction

Getting Started with Aspects for JavaScript

An aspect of a program is a feature linked to many other parts of the program, but which is not related to the program's primary function. Jaspect let's you write these aspects separately and then add them in at the last minute.

The canonical logging example

This some code that you might write:

// in myStuff.js

var myMult = function(x, y, z){
    return x * y * z;
}

myMult(myMult(2,3,4),2,3);

To log before every call to myMult:

// in loggingAspect.js

module.exports = function(jaspect){

  var context = {};
  var multPointCut = {type:"call", name:"myMult"};
  
  jaspect.before(multPointCut, context, function(joinPoint){
        
        console.log("DEBUG " + joinPoint.name + joinPoint.args);
        
  });
}

Finally to apply the aspect to your code:

jaspect -j loggingAspect.js myStuff.js

When the resulting file is run we get the following output:

DEBUG myMult 2,3,4
DEBUG myMult myMult(2,3,4), 2, 3

Installation

npm install -g jaspect

Definitions

Aspect

An aspect of a program is a feature linked to many other parts of the program, but which is not related to the program's primary function.

Join Point

A specific point in the control flow of your program.

Cut Point

A set of join points.

Advice

A piece of code to be executed on all join points in a cut point.

Documentation

Point Cuts

Advice

Usage

Limitations

Contributing

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.