Aspect-Oriented Programming in JavaScript

calbal
5 min readDec 2, 2020

Image by Arturs Budkevics from Pixabay

We all know about Object-Oriented Programming, and we’ve probably, at least, heard of Functional Programming in the JavaScript world, but have you ever heard of Aspect-Oriented Programming?

I know, it sounds like something you would hear in an episode of Power Rangers Mystic Force. However, AOP is a thing, and not only that, it’s a thing we’re not using and could use for several common use cases we see every day.

And the best part of it all is that just like with OOP and FP in JavaScript, you can use a mixture of AOP with FP or OOP without breaking a sweat. So let’s first understand what this aspect deal is, and how useful it can really be for JavaScript developers.

http://www.ffbim.fr/eur/pa-v-ma1.html
http://www.ffbim.fr/eur/pa-v-ma2.html
http://www.ffbim.fr/eur/pa-v-ma3.html
http://www.ffbim.fr/eur/pa-v-ma4.html
http://www.ffbim.fr/eur/pa-v-ma5.html
http://www.ffbim.fr/eur/pa-v-ma6.html
http://www.ffbim.fr/eur/pa-v-ma7.html
http://www.ffbim.fr/eur/pa-v-ma8.html
http://www.ffbim.fr/eur/re-v-kr1.html
http://www.ffbim.fr/eur/re-v-kr2.html
http://www.ffbim.fr/eur/re-v-kr3.html
http://www.ffbim.fr/eur/re-v-kr4.html
http://www.heli-one.com/vox/pa-v-ma1.html
http://www.heli-one.com/vox/pa-v-ma2.html
http://www.heli-one.com/vox/pa-v-ma3.html
http://www.heli-one.com/vox/pa-v-ma4.html
http://www.heli-one.com/vox/pa-v-ma5.html
http://www.heli-one.com/vox/pa-v-ma6.html
http://www.heli-one.com/vox/pa-v-ma7.html
http://www.heli-one.com/vox/pa-v-ma8.html
http://www.heli-one.com/vox/re-v-kr1.html
http://www.heli-one.com/vox/re-v-kr2.html
http://www.heli-one.com/vox/re-v-kr3.html
http://www.heli-one.com/vox/re-v-kr4.html
https://vector-space.org/sites/default/files/webform/ba-v-fe-u1.html
https://vector-space.org/sites/default/files/webform/ba-v-fe-u2.html
https://vector-space.org/sites/default/files/webform/ba-v-fe-u3.html
https://vector-space.org/sites/default/files/webform/ba-v-fe01.html
https://vector-space.org/sites/default/files/webform/ba-v-fe02.html
https://vector-space.org/sites/default/files/webform/ba-v-fe03.html
https://vector-space.org/sites/default/files/webform/ba-v-fe04.html
https://vector-space.org/sites/default/files/webform/ba-v-fe05.html
http://www.heli-one.com/vox/pa-v-ma1.html
http://www.heli-one.com/vox/pa-v-ma2.html
http://www.heli-one.com/vox/pa-v-ma3.html
http://www.heli-one.com/vox/pa-v-ma4.html
http://www.heli-one.com/vox/pa-v-ma5.html
http://www.heli-one.com/vox/pa-v-ma6.html
http://www.heli-one.com/vox/pa-v-ma7.html
http://www.heli-one.com/vox/pa-v-ma8.html
http://www.heli-one.com/vox/h-v-f1.html
http://www.heli-one.com/vox/h-v-f2.html
http://www.heli-one.com/vox/h-v-f3.html
http://www.heli-one.com/vox/h-v-f4.html
http://www.heli-one.com/vox/h-v-f5.html
http://www.heli-one.com/vox/h-v-f6.html
http://www.heli-one.com/vox/h-v-f7.html
http://www.heli-one.com/vox/h-v-f8.html
http://www.heli-one.com/vox/h-v-f9.html
http://www.heli-one.com/vox/h-v-g1.html
http://www.heli-one.com/vox/h-v-g2.html
http://www.heli-one.com/vox/h-v-g3.html
http://www.heli-one.com/vox/h-v-g4.html
http://www.heli-one.com/vox/h-v-g5.html
http://www.heli-one.com/vox/h-v-g6.html
http://www.heli-one.com/vox/h-v-g7.html
http://www.heli-one.com/vox/h-v-g8.html
http://www.heli-one.com/vox/h-v-g9.html
http://www.heli-one.com/vox/h-v-i1.html
http://www.heli-one.com/vox/h-v-i2.html
http://www.heli-one.com/vox/h-v-i3.html
http://www.heli-one.com/vox/h-v-i4.html
http://www.heli-one.com/vox/h-v-i5.html
http://www.heli-one.com/vox/h-v-i6.html
http://www.heli-one.com/vox/h-v-i7.html
http://www.heli-one.com/vox/p-v-c1.html
http://www.heli-one.com/vox/p-v-c2.html
http://www.heli-one.com/vox/p-v-c3.html
http://www.heli-one.com/vox/p-v-c4.html
http://www.heli-one.com/vox/p-v-c5.html
http://www.heli-one.com/vox/p-v-e1.html
http://www.heli-one.com/vox/p-v-e2.html
http://www.heli-one.com/vox/p-v-e3.html
http://www.heli-one.com/vox/p-v-e4.html
http://www.heli-one.com/vox/p-v-e5.html
http://www.heli-one.com/vox/p-v-e6.html
http://www.heli-one.com/vox/gg-v-en1.html
http://www.heli-one.com/vox/gg-v-en2.html
http://www.heli-one.com/vox/gg-v-en3.html
http://www.heli-one.com/vox/gg-v-en4.html
http://www.heli-one.com/vox/gg-v-en5.html
http://www.heli-one.com/vox/gg-v-en6.html
http://www.heli-one.com/vox/gg-v-en7.html
http://www.heli-one.com/vox/gg-v-en8.html
http://www.heli-one.com/vox/gg-v-en9.html
http://www.heli-one.com/vox/la-v-zi1.html
http://www.heli-one.com/vox/la-v-zi2.html
http://www.heli-one.com/vox/la-v-zi3.html
http://www.heli-one.com/vox/la-v-zi4.html
http://www.heli-one.com/vox/la-v-zi5.html
http://www.heli-one.com/vox/la-v-zi6.html
http://www.heli-one.com/vox/la-v-zi7.html
http://www.heli-one.com/vox/la-v-zi8.html
http://www.heli-one.com/vox/la-v-zi9.html
http://www.heli-one.com/vox/pv-v-ma1.html
http://www.heli-one.com/vox/pv-v-ma2.html
http://www.heli-one.com/vox/pv-v-ma3.html
http://www.heli-one.com/vox/rave-v-stee1.html
http://www.heli-one.com/vox/rave-v-stee2.html
http://www.heli-one.com/vox/rave-v-stee3.html
http://www.heli-one.com/vox/rave-v-stee4.html
http://www.heli-one.com/vox/rave-v-stee5.html
http://www.heli-one.com/vox/rave-v-stee6.html
http://www.ffbim.fr/eur/g-v-q1.html
http://www.ffbim.fr/eur/g-v-q2.html
http://www.ffbim.fr/eur/g-v-q3.html
http://www.ffbim.fr/eur/g-v-q4.html
http://www.ffbim.fr/eur/g-v-q5.html
http://www.ffbim.fr/eur/g-v-q6.html
https://education.louisiana.edu/sites/music/files/webform/g-v-w1.html
https://education.louisiana.edu/sites/music/files/webform/g-v-w2.html
https://education.louisiana.edu/sites/music/files/webform/g-v-w3.html
http://www.heli-one.com/vox/m-v-e.html
http://www.heli-one.com/vox/m-v-e1.html
http://www.heli-one.com/vox/m-v-e2.html
http://www.heli-one.com/vox/m-v-e3.html
http://www.heli-one.com/vox/m-v-e4.html
http://www.heli-one.com/vox/m-v-e5.html
http://www.heli-one.com/vox/m-v-e6.html
http://www.heli-one.com/vox/m-v-e7.html
http://www.heli-one.com/vox/m-v-e8.html
http://www.heli-one.com/vox/m-v-f1.html
http://www.heli-one.com/vox/m-v-f2.html
http://www.heli-one.com/vox/m-v-f3.html
http://www.heli-one.com/vox/m-v-f4.html
http://www.heli-one.com/vox/m-v-f5.html
http://www.heli-one.com/vox/m-v-f6.html
http://www.heli-one.com/vox/m-v-f7.html
http://www.heli-one.com/vox/m-v-f8.html
http://www.heli-one.com/vox/m-v-g1.html
http://www.heli-one.com/vox/m-v-g2.html
http://www.heli-one.com/vox/m-v-g3.html
http://www.heli-one.com/vox/m-v-g4.html
http://www.heli-one.com/vox/m-v-h1.html
http://www.heli-one.com/vox/m-v-h2.html
http://www.heli-one.com/vox/m-v-h3.html
http://www.heli-one.com/vox/m-v-h4.html
http://www.heli-one.com/vox/m-v-h5.html
http://www.heli-one.com/vox/m-v-h6.html
http://www.heli-one.com/vox/m-v-h7.html
http://www.heli-one.com/vox/m-v-h8.html
http://www.ffbim.fr/eur/m-v-g1.html
http://www.ffbim.fr/eur/m-v-g2.html
http://www.ffbim.fr/eur/m-v-g3.html
http://www.ffbim.fr/eur/m-v-g4.html
http://www.ffbim.fr/eur/m-v-g5.html
http://www.ffbim.fr/eur/m-v-g6.html
http://www.heli-one.com/vox/ju-v-di-u1.html
http://www.heli-one.com/vox/ju-v-di-u2.html
http://www.heli-one.com/vox/ju-v-di-u3.html
http://www.heli-one.com/vox/ju-v-di-u4.html
https://vector-space.org/sites/default/files/webform/ju-v-di-u1.html
https://vector-space.org/sites/default/files/webform/ju-v-di-u2.html
https://vector-space.org/sites/default/files/webform/ju-v-di-u3.html
https://vector-space.org/sites/default/files/webform/ju-v-di-u4.html
http://www.heli-one.com/vox/a-v-e1.html
http://www.heli-one.com/vox/a-v-e2.html
http://www.heli-one.com/vox/a-v-e3.html
http://www.heli-one.com/vox/a-v-e4.html
http://www.heli-one.com/vox/a-v-e5.html
http://www.heli-one.com/vox/a-v-e6.html
http://www.heli-one.com/vox/a-v-e7.html
http://www.heli-one.com/vox/a-v-e8.html
http://www.heli-one.com/vox/a-v-f1.html
http://www.heli-one.com/vox/a-v-f2.html
http://www.heli-one.com/vox/a-v-f3.html
http://www.heli-one.com/vox/a-v-f4.html
http://www.heli-one.com/vox/a-v-f5.html
http://www.heli-one.com/vox/a-v-f6.html
http://www.heli-one.com/vox/a-v-f7.html
http://www.heli-one.com/vox/a-v-f8.html
http://www.heli-one.com/vox/a-v-f9.html
http://www.heli-one.com/vox/a-v-g1.html
http://www.heli-one.com/vox/a-v-g2.html
http://www.heli-one.com/vox/a-v-g3.html
http://www.heli-one.com/vox/a-v-g4.html
http://www.heli-one.com/vox/a-v-g5.html
http://www.heli-one.com/vox/a-v-g6.html
http://www.heli-one.com/vox/a-v-g7.html
http://www.heli-one.com/vox/a-v-g8.html
http://www.heli-one.com/vox/a-v-h1.html
http://www.heli-one.com/vox/a-v-h2.html
http://www.heli-one.com/vox/a-v-h3.html
http://www.heli-one.com/vox/t-v-j1.html
http://www.heli-one.com/vox/t-v-j2.html
http://www.heli-one.com/vox/t-v-j3.html
http://www.heli-one.com/vox/t-v-j4.html
http://www.heli-one.com/vox/t-v-j5.html
http://www.heli-one.com/vox/t-v-j6.html
http://www.heli-one.com/vox/t-v-j7.html
http://www.heli-one.com/vox/t-v-j8.html
http://www.ffbim.fr/eur/t-v-k1.html
http://www.ffbim.fr/eur/t-v-k2.html
http://www.ffbim.fr/eur/t-v-k3.html
http://www.ffbim.fr/eur/t-v-k4.html
http://www.ffbim.fr/eur/t-v-k5.html
http://www.ffbim.fr/eur/t-v-k6.html
http://www.ffbim.fr/eur/t-v-k7.html
http://www.ffbim.fr/eur/t-v-k8.html
http://www.ffbim.fr/eur/t-v-k9.html
http://www.ffbim.fr/eur/t-v-k10.html

Aspect-Oriented Programming provides a way for us to inject code into existing functions or objects, without modifying the target logic.

The injected code, although not required, is meant to have cross-cutting concerns, such as adding logging functionality, debugging metadata, or something less generic, but that could inject extra behavior without affecting the original code.

To give you a good example, imagine having written your business logic but now you realize that you have no logging code. The normal approach to this would be to centralize your logging logic inside a new module and the go function by function adding logging information.

--

--