Source Code

Download: hashCode-v1.0.0.js (save as)
/**
 * Javascript HashCode v1.0.0
 * This function returns a hash code (MD5) based on the argument object.
 * http://pmav.eu/stuff/javascript-hash-code
 *
 * Example:
 *  var s = "my String";
 *  alert(HashCode.value(s));
 *
 * pmav, 2010
 */
var HashCode = function() {

    var serialize = function(object) {
        // Private
        var type, serializedCode = "";

        type = typeof object;

        if (type === 'object') {
            var element;

            for (element in object) {
                serializedCode += "[" + type + ":" + element + serialize(object[element]) + "]";
            }

        } else if (type === 'function') {
            serializedCode += "[" + type + ":" + object.toString() + "]";
        } else {
            serializedCode += "[" + type + ":" + object+"]";
        }

        return serializedCode.replace(/\s/g, "");
    };

    // Public, API
    return {
        value : function(object) {
            return MD5(serialize(object));
        }
    };
}();
(function(){

    Main = {

        log : function(s) {
            document.body.innerHTML += '

'+(new Date())+": "+s+'

'; }, init : function() { var object1 = []; var object2 = { f1 : function() { var i; }, a : [1, 2, "#", { m : function() { return 1; } }]}; var object3 = object2; var object4 = function(arg) { arg++ }; var object5 = function(arg) { arg++ }(2); var object6 = (function(arg) { arg++ })(2); this.log("Object #1 - "+HashCode.value(object1)); this.log("Object #2 - "+HashCode.value(object2)); this.log("Object #3 - "+HashCode.value(object3)); this.log("Object #4 - "+HashCode.value(object4)); this.log("Object #5 - "+HashCode.value(object5)); this.log("Object #6 - "+HashCode.value(object6)); } }; })()