T
his tutorial is a guide to working at the code level with AspectJS. Starting with first principles,
it covers the basics of applying intercepts to functions, before progressing to the application of multiple interceptions
to a given method. From there, it explores the use of those ideas in conjunction with the various flavours of
object-method that JavaScript supports, before examining exotic applications of the technology, along with useful tips and
possible abuses of function interception. Detailed examples are provided throughout to illustrate the
principles examined.
Purview and Intent
The tutorial is a guide to how to do a particular thing with AspectJS, not an exploration of why doing that might
be useful in a web site or application. Given this, it does not explore the application of the technology in the
wider sense, or within the scope of 'Aspect-Oriented Programming' as an approach to software development. Indeed,
some of the principles explained may have no useful application, while others may have unrealised potential.
Similarly, the tutorial explores aspects of JavaScript itself only incidentally, and is not a general-purpose
guide to the language. Readers are therefore advised to consult a good reference on JavaScript wherever
language-specific concepts that require further explanation are mentioned.
The information contained in this tutorial relates to version 1.0 of all products that
Dodeca Technologies Ltd provides under the name of 'AspectJS', and the version number of the tutorial is 0.3.
CSS-Rendering and Nomenclature
Where JavaScript keywords, and type, object and function names appear within body text they are (or should be)
rendered in a mono-spaced, sans-serif typeface, and should appear thus. This
means that when the terms AJS or AJS_HP
appear in such a typeface they refer to those run-time objects, as defined in the relevant .js
files.
Where, however, the term 'AspectJS' appears in the same typeface as the body text, it refers to the AspectJS-product
as a whole, rather than any particular run-time artefact. In that sense, the term means both the general purpose and
high-performance versions of the technology, unless stated otherwise.
Sample Code
Note that the code examples are deliberately simple, which makes most of them somewhat dull and uninteresting in themselves.
The aim, however, is to ensure that the key principle under consideration is shown clearly, and not obscured by other
issues in the listing. Accordingly, do note that, while the examples presented here often use affix functions that are
called 'Prefix' or 'Suffix', these names are used purely for convenience — affixes can have any name that conforms
to JavaScript's function-naming rules (e.g. a name must not be a reserved word, must not start with a numeric character etc).
Note also that a very few listings will not run, and are presented so as to illustrate that fact. Such
non-working listings are identified in the code concerned.
Testing the Examples
All code samples here have been tested on Firefox 1, Firefox 2, and Internet Explorer 6. To experiment with the code yourself,
perform a click-and-drag across a given listing, copy it, paste into your editor, and proceed from there.
Alternatively, you can download the samples from here as a Zip file that contains the complete set of examples
(including the non-working ones), who's numbering corresponds to the numbers within the comments in the
tutorial code. Once downloaded, decompress the Zip-file contents to a directory of your choice, and go
from there — note that an instance of the AspectJS sources should also reside in that directory for the
examples to run correctly.
All the examples are hard-coded to load AspectJS_Reference.js (rather than AspectJS_HP_Reference.js), and therefore
demonstrate only the AJS object when run straight out of the box. To run them using the
AJS_HP object, rename AspectJS_HP_Reference.js to AspectJS_Reference.js, then
load that file into your editor, and put the following line of code at the end of the file:
var AJS = AJS_HP;
The examples can then be tested from there, and will all use the AJS_HP object.
Do remember to perform this on a copy of AspectJS that is separate from the one that you deploy, otherwise your
production code could end up using AJS_HP inadvertently, and the
restrictions on that object's functionality could then cause you to think erroneously that there is a bug in your
application.
Copyright
Note that, while the technology that comprises AspectJS is available under a free license, the copyright to the
text and graphics that comprise this tutorial does not fall within the scope of that license, and remains with
Dodeca Technologies Ltd.