Add hash code method to String - Node.js String

Node.js examples for String:String Value

Description

Add hash code method to String

Demo Code

String.prototype.hashCode = function() {
    var hash = 0,
        i, chr, len;//from  w  ww  .ja  v  a2  s . c o  m
    if (this.length == 0) return hash;
    for (i = 0, len = this.length; i < len; i++) {
        chr = this.charCodeAt(i);
        hash = ((hash << 5) - hash) + chr;
        hash |= 0; // Convert to 32bit integer
    }
    return hash;
};

Related Tutorials