/**
core.js
version 1.2
author treeroot
since 2005-5-24
*/
//Object
Object.prototype.getClass=function(){
//var s=Object.prototype.toString.apply(this);
//return s.match(/\[object (\w+)\]/)[1];
//just work for system class
var s=this.constructor.toString();
return s.match(/function (\w+)\(.+/)[1];
}
Object.prototype.hashCode=function(){
var h=0;
var s=this.toString();
for (var i = 0; i < s.length; i++) {
h = 31*h + s.charCodeAt(i);
}
return h;
}
//protected used in subclass
Object.prototype.typeMatches=function(obj){
return this.getClass()===obj.getClass();
}
//
Object.prototype.equals=function(obj){
if(!this.typeMatches(obj)) return false;
return this.toString()===obj.toString();
}
//String
String.prototype.equalsIgnoreCase=function(str){
if(str.getClass()!="String") return false;
return this.toUpperCase()===str.toUpperCase();
}
String.prototype.compareTo=function(str){
if(!this.typeMatches(str)) throw "Type Mismacth!";
var s1=this.toString();
var s2=str.toString();
if(s1===s2) return 0;
else if(s1>s2) return 1;
else return -1;
}
String.prototype.compareToIgnoreCase=function(str){
if(!this.typeMatches(str)) throw "Type Mismacth!";
var s1=this.toUpperCase();
var s2=str.toUpperCase();
if(s1===s2) return 0;
else if(s1>s2) return 1;
else return -1;
}
String.prototype.startsWith=function(prefix){
return this.substring(0,prefix.length)==prefix;
}
String.prototype.endsWith=function(suffix){
return this.substring(this.length-suffix.length)==suffix;
}
String.prototype.concat=function(str){
return new String(this.toString()+str);
}
String.prototype.toCharArray=function(){
var charArr=new Array();
for(var i=0;i<this.length;i++) charArr[i]=this.charAt(i);
return charArr;
}
String.prototype.trim=function(){
return this.replace(/(^\s*)|(\s*$)/g,"");
}
//Number
Number.prototype.hashCode=function(){
//just for int,not for double
return (this);
}
Number.prototype.equals=function(obj){
if(!this.typeMatches(obj)) return false;
return this.toString()==obj.toString();
}
Number.prototype.compareTo=function(obj){
if(!this.typeMatches(obj)) return false;
return this-obj;
}
Number.toHexString=function(i){
return i.toString(16);
}
Number.toBinaryString=function(i){
return i.toString(2);
}
//Date
Date.prototype.hashCode=function(){
var l=this.getTime();
var s=Number.toHexString(l);
var high=0;
if(s.length>8) high=parseInt(s.substring(0,s.length-8),16);
var low=l & 0xffffffff;
return low^high;
}
Date.prototype.equals=function(obj){
if(!this.typeMatches(obj)) return false;
return this.getTime()==obj.getTime();
}
Date.prototype.compareTo=function(obj){
if(!this.typeMatches(obj)) return false;
return (this.getTime()-obj.getTime())& 0xffffffff;
}
转自:http://blog.csdn.net/treeroot/archive/2005/05/24/379720.aspx