Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Design

Transparency on Demand


Optimum Implementation

This technique can be generalized to allow the arbitrary interception and loading of methods and libraries respectively, and employing a function that contains an inner function is the optimum approach here. In this scheme, the outer function takes arguments denoting the interceptee, its owner-object, and the name of the library to be loaded; on execution, it replaces the interceptee-reference in the owner-object with a reference to the inner function.

This forms a closure (and what a superb use for those exotic creatures) that makes the outer function's parameters available to the inner function when it executes. It follows that calling the interceptee invokes the inner function, which calls the library-loader before replacing the reference to itself with the original reference to the interceptee. Listing Five (available online; see "Resource Center," page 5) demonstrates this.

AspectJS

The reality is that function interception in JavaScript constitutes a form of meta-programming in an interpreted language, and has many aspect-oriented applications beyond loading on demand. Given that wider context, you can view a call to a library-loader as a "prefix" to the interceptee, and this introduces the notion of functions that act as "suffixes," where a method is called after, rather than before, the invocation of the interceptee.

This leads to the notion of "wrapping" an interceptee with such affixes, which leads from there to the question of affix lifetime. In the on-demand example, the prefix executes just once before it is detached, but there may be instances where an affix should execute for a certain number of times, or even infinitely; the ability to remove an affix explicitly is also desirable. Moreover, multiple affixes may be desirable—in Listing Two there are three loading calls, which suggests applying three discrete prefixes to DisplayMenu, and this introduces the notion of changing their execution order if needed. Then there is the question of exception handling, error checking, and diagnostics, as well as performance, all of which raise significant design questions.

A clear case exists, therefore, for a general-purpose function-interception library that addresses these points, thereby obviating re-invention of the wheel; and AspectJS (www.dodeca.co.uk/ajs_About_AspectJS.htm) is one such resource that is freely available. Encapsulating the mechanics of function interception, it allows (among other things) unlimited numbers of affixes for a given function, with fine control over their type, number, longevity, and execution order. In deference to a venerable tradition, therefore, Listing Six (available online) demonstrates loading a JavaScript library transparently on demand using AspectJS.

This uses the AddPrefix method of a singleton object called AJS to set up the interception. The first two parameters signify the OnMouseClick method of the global object as the interceptee, and the third specifies LoadLib_XHR as the prefix. The fourth is an optional parameter that is passed to the prefix—in this case the name of the library to be loaded—and the final argument specifies that the prefix should be removed after just one execution. The net result is that clicking on the client area of the browser's window causes Hello.js to be loaded, thus allowing a customary greeting to be displayed.

An Open Door

Resolving the downside to on-demand JavaScript lets you exploit its full potential, and generalizing the solution opens the door to a wealth of aspect-oriented techniques. Consolidating the principles of function interception in a reusable library lets you affix methods arbitrarily to virtually any function call. This permits the serious application of AOP techniques to professional web development, thus allowing remote tracing, debugging, and error reporting, along with client-side performance measurement, user-interaction modeling, and others that, as yet, are only a twinkle in the developer's eye.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.