/ Published in: JavaScript
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
;(function() { MyClass = { map: function(object, callback) { var items = []; each(object, function(i) { items.push(callback.apply(this, arguments)); }); return items; }, each: function(object, callback) { for (var i = 0; i < object.length; i++) { callback(i); } } } var methods = 'each map'.split(/ /); while (methods.length) { eval('var ' + methods[0] + ' = MyClass.' + methods.shift()); } })(); // Map can refer to each without dereferencing result = MyClass.map([1, 2, 3], function(i) { return i + 1; }); print(result); // 1,2,3
URL: http://dailyjs.com/2010/03/19/private-vars-scope