Skip to content

Instantly share code, notes, and snippets.

@nfreear
Created February 9, 2012 09:35
Show Gist options
  • Select an option

  • Save nfreear/1778825 to your computer and use it in GitHub Desktop.

Select an option

Save nfreear/1778825 to your computer and use it in GitHub Desktop.
Javascript utility functions, for Dev8D workshop, by Juliette Culver | http://data.dev8d.org/2012/programme/event/CS21
/**
* Javascript create function - safe use of 'new'.
* By Juliette Culver.
*/
function create(parent) {
f = function();
f.prototype = parent;
a = new f();
return a;
}
/**
* Simple Javascript command-line dump function (use with eg. JSDB).
* By Juliette Culver.
*/
function dump(obj) {
for(var attr in obj) {
println(attr+':'+obj[attr]);
}
}
/**
* A full Javascript command-line dump function, like PHP's var_dump.
* (Better for strings, arrays, boolean types..)
* By Juliette and Nick.
*/
function var_dump(o) {
var
t=typeof o,
l=o.length;
print(t);
if(l) {
if('object'==t)print(':array');
return println('('+l+') : '+o); // Return arrays, strings (and functions).
}
if('object'!=t)return println(' : '+o); // Return everything but true objects.
println(' {');
for(var at in o) {
println(at+' : '+o[at]);
}
println('}'); // Finally, true objects.
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment