Top
Simple metaprogramming example in JavaScript
function DataViewer() { this.renderers = {}; this.renderers.table = function(data) { ... }; this.renderers.tree = function(data) { ... }; } DataViewer.prototype.render = function(type, data) { if (!this.renderers.hasOwnProperty(type)) throw('Unknown data type.'); return this.renderers[type](data); }jQuery counter with closures
function count(data) { var count = 0; jQuery.each(data, function() { count++; }); return count; }