javescript string generates hash value

It is used to compare whether the string is consistent, generally to determine whether the existing text has been edited again.

function hashVal(string){
    
    
	var hash = 0, i, chr;
	if (string.length === 0) return hash;
	for (i = 0; i < string.length; i++) {
    
    
	chr = string.charCodeAt(i);
	hash = ((hash << 5) - hash) + chr;
	hash |= 0; // Convert to 32bit integer
	}
    return hash;
}
let string = '你好有缘人';
console.log(hashVal(string)); // -2000030674

Guess you like

Origin blog.csdn.net/weixin_43299180/article/details/112566057