Category Archives: Nerd

Finish Writing Me Plz Nerd

print_r() for javascript

I typically just use uneval() to figure out what’s inside an array/object, but what about when it’s large and heterogeneous? I wanted to find a version of php’s print_r() for JavaScript. Here is link to the Original Version of dump() I based mine off of:

www.openjs.com/scripts/others/dump_function_php_print_r.php

When I tried it, the first thing I noticed what that it put quotes around everything, and that long strings with line breaks got messy, so I did a quick adaptation to suit my immediate needs and came up with this:

function dump(arr,level) {
  function magicquotes(value) { return (isNaN(value)) ? '"' + value.replace(/\n/g,"\n"+indent) + '"' : value; }
  level = level | 0;
  var indent = new Array(level+1).join("\t"), dumped_text = "";

  if(typeof(arr) == 'object') { //Array/Hashes/Objects
    for(var item in arr) {
      var value = arr[item];

      if(typeof(value) == 'object')
          dumped_text += indent + "'" + item + "' :\n" + dump(value,level+1);
      else
          dumped_text += indent + "'" + item + "' => " + magicquotes(value) + "\n";
    }
  } else { //Stings/Chars/Numbers etc.
    dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
  }
  return dumped_text;
}

I also found links to many other print_r(), var_dump() equivalents, but they either depended on write, were overly complicated, or returned a lot of excess text I wasn’t interested in.

So why not just call it print_r ? Well, the original I copied was called dump, and I’ve always been annoyed typing that underscore, so I just didn’t change it.

Update: found another alternative. It’s really long and puts out a lot of extraneous information, but it’s worth looking at: wiki.greasespot.net/Code_snippets#Dump_the_properties_of_an_object

Nerd

nano review of JavaScript frameworks (YUI, jQuery, moo)

Having gone through these three frameworks, I’m going to say something I wish other review sites said: For each purpose, there is a “best” framework. It all depends on context.

The Yahoo! User Interface Library (YUI)

If you’re building a rich web application from scratch, YUI will have very thorough tools to do anything you want. As long as you survive writing the code.

Mootools

If you’re looking to add a little ajax and dhtml to your exisiting web page, or need a lightweight, high performance library, go with Mootools.

jQuery

Somewhere between Mootools and YUI, jQuery has a lot of  features and is very tidy to code.

Perhaps we can compare the philosophies behind each framework by examining their respective homepages:

YUI jQuery Mootools
YUI jquery moo
Slogan The YUI Library is a set of utilities and controls, written in JavaScript, for building richly interactive web applications using techniques such as DOM scripting, DHTML and AJAX. The Write Less, Do More, JavaScript Library a compact javascript framework
Size 8.91KB 13.41KB 7.33KB
Comment Look at how busy and dated their homepage is… but it is compact. A very slick page, but underneath it, it is a little bloated. Simple and clean.