After coded enough Javascript few months back, I found that there are a couple of functions that I kept re-using in different projects. Therefore I took some time to refactor them and re-arrange them into a single file. The common code that I keep reusing even today consists of functions that does prototypical inheritance, scope maintenance, some jquery stuff, google maps api stuff and some general ajax application related code.
PrototypicalPrototypal Inheritance
Basically after reading through some articles across the internet, especially this article, then I have this piece of code in my collection.
if(typeof Object.create !== 'function') {
Object.create = function(_o) {
var F = function () {}
F.prototype = _o;
return new F();
};
}
It is basically the same piece of code because I find it cleaner and more efficient than my initial draft.
Namespacing
Everyone agrees that global variables are evil, therefore I group all my common functions and classes into one single object, as follows,
var cs = Object.create(new function() {
// some other code
}
And I find the way YUI manages the modules good enough for me, then I emulated YAHOO.namespace function by having this in my object.
this.namespace = function(_namespace) {
if(!this[_namespace]) {
this[_namespace] = Object.create(new function() {});
}
return this[_namespace];
}
The code may not be clever/clean, but as long as it does the job I have no complaint.
Preserving State
I didn’t quite like the delegate function as I can’t pass in extra arguments, therefore I have this after some refactoring
var __delegate = function(_method, _scope) {
var _arguments = arguments;
return function() {
return _method.apply(
_scope, compile_arguments.call(
this, _arguments, [], 2
));
}
};
var compile_arguments = function(_arguments, _result, _start) {
for(var i = _start; _arguments.length > _start && i < _arguments.length; i++) {
_result.push(_arguments[i]);
}
return _result;
};
this.delegate = __delegate;
Emulate Functional Programming
I have recently decided to learn a little bit on functional programming recently but I haven’t decide which language I am picking. I have some experience in SWI-Prolog but prolog is just a declarative functional programming and does not seem to offer closure, higher order functions and currying. Anyway, back to the topic, after spending some time in this book, I modified some code in the book and result in…
this.functional = Object.create(new function() {
this.map = function(_function, _arguments) {
var result = [];
for(var i = 0; i < _arguments.length; i++) {
result.push(_function(_arguments[i]));
}
return result;
};
this.reduce = function(_combine, _base, _arguments) {
return _arguments.length > 0 ?
this.reduce(
_combine, _combine(_base, _arguments.shift()),
_arguments
) : _base;
};
});
I am not sure if I can do the similar recursion in cs.functional.map compared to cs.functional.reduce because I am rather weak in recursion.
That’s all for now, and in the second part of this article I will post some helper functions for use with some third party libraries like jQuery and Google Maps API.
EDIT 15/8/2009 There are some changes to this set of functions, will post an update soon and there will probably no part 2 any time soon.