javascript函数库:core.js

王朝html/css/js·作者佚名  2006-01-09
宽屏版  字体: |||超大  

/**

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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有