Skip to main content


Showing posts from August, 2012

Array magic in JavaScript

I was trying to create a queue to collect all the element related functions to run after the elements had been created. As I was Googling, I came by this snippet. var calls = []; function executeNext(next) { if(calls.length == 0) return; var fnc = calls.pop(); fnc(); if(next) { executeNext(true); } } /*To call method chain synchronously*/ calls.push(callMe3); calls.push(callMe2); calls.push(callMe1); executeNext(true); /*To call method chain asynchronously*/ calls.push(callMe3); calls.push(function(){ callMe2(); executeNext(false); }); calls.push(function(){ callMe1(); executeNext(false); }); An array was a perfect fit with the built in push() and pop() function. I also needed something that would work the pop() till the array was empty. As Douglas Crockford says in his wonderful book 'JavaScript: the good parts', arrays are actually (slightly disguised) objects. So one can simple add to the native methods that the array varia…