Created
February 9, 2012 09:35
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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