Functional JavaScript

Most of the time my focus is on OOP JavaScript, and I provide very little discussion of functional JavaScript. This is because although JavaScript is not a traditional OOP language, it is an OOP language (a prototype based one). I prefer to leverage the OOP structure of the language, however JavaScript is very versatile and you are not limited to one coding style. Functional programming is one of these styles, it is a technique where one uses global methods to manipulate basic data structures, such as strings, integers, objects, etc. (PHP < v5 is a good example of a function-driven programming language).

Consider for example the trim method that removes the surrounding whitespace from strings. Many developers and libraries choose to add trim to the prototype of the String Object:

Example 1: Prototype-Based Trim

String.trim = function() {
	return this.remove(\/^\s\s*\/).remove(\/\s\s*$\/);

In functional programming one would write the same function as follows:

Example 2: Functional Trim

string_trim = function(str) {
	return str.remove(\/^\s\s*\/).remove(/\s\s*$\/);

The work of the method hasnt changed; we are still doing two regular expression removes on a String. However the method structure has. First, because we are putting the method in the global namespace, we need to use a naming convention. In example 2, I have used the naming convention, nameOfObject + _ + nameOfFunction, with the first letter of each part lower-case, and all subsequent words camel-case (not shown in example, since trim is a single word). The Object name is used as a prefix, because you may have multiple trim functions (perhaps an array_trim that removes empty elements from an Array). Also, in Example 2, we pass the String Object into the Function instead of referencing this as done in Example 1.

Those are the two major differences between Functional and OOP programming in JavaScript. In OOP you will extend prototype and your methods will directly reference the instantiated Object with the this keyword. In Functional JavaScript you will call global methods and pass primitive objects around, ideally never need to the this keyword.

My initial thoughts on Functional JavaScript produced these considerations:

Functional JavaScript will be slower than OOP
This is probably just a knee-jerk reaction, because I dont see why this would be true. Functions are executed quickly in JavaScript, whether attached to an Object or referenced by a closure. It is true that you will use slightly fewer characters using the OOP method, because the names of the functions are generally shorter. However, we attach fewer methods to primitive objects with the Functional method, so each primitive Object instantiation will be faster and lighter.
Functional JavaScript will have namespace conflicts
While this could be an issue, with a good naming convention it can be easily avoided. I recommend extending the method used in Example 2 to also include the authors initials (nameOfObject + _ + authorsInitials + _ + nameOfFunction). This naming convention allows you to have multiple methods for the same operation, in-case you needed that. And also gives the JavaScript community an idea of the author of a given method.
What About the Module Pattern and encapsulation
While, you wont be able to leverage all the goodness of the module pattern, because true Functional JavaScript will not return an instance of this, as the module pattern does. You still can benefit from the use of anonymous functions to encapsulate your business logic and avoid local namespace conflicts.
Are there Frameworks written for Functional JavaScript
As far as I know, there are none. I would be interested in seeing how effective this pattern works on the library level. However, it would be a colossal undertaking.

I do not believe there is a drastic performance improvement with either technique; however one would need two comparable libraries using the different techniques and run many tests against each to verify this. I believe the decision on whether you use Functional or OOP JavaScript comes down to your own coding preference and whether you want to use an existing framework or not.

Peter Michaux also wrote a good article about JavaScript Namespacing, if you are interested in additional reading. If you have experience with Functional JavaScript libraries and would like to share, please post a comment.