AOP in JavaScript, with Prototype

Posted by mgroves on February 23, 2012  •  Comments (0)  •  Tags: JavaScript, Prototype

JavaScript is an object-oriented programming language. (This may be an overly simplistic statement). As an object oriented programming language, it's subject to some of the same pitfalls that other object-oriented languages have, in terms of boilerplate, cross-cutting concerns, etc. With that in mind, AOP is a tool that can be used to address these issues in the JavaScript portion of your application.

For instance, the Prototype JS framework has a wrap function that you can use to implement "the essence of AOP".

Here's a self-contained Hello World example:

A trivial example, but notice that JavaScript's dynamic/functional paradigm gives you the ability to "wrap" a function instead of actually "weaving" in a cross-cutting concern. In this example, prototype's capitalize function is being replaced, which is very similar to an interception aspect, and the underlying capitalize method is being called within that interceptor/wrapper.

Leave a Comment

* = Required
*
*

Some HTML will be escaped, you can use StackOverflow-flavored Markdown