首 页 | 新 闻 | 技术中心 | 第二书店 | 《程序员》 | 《开发高手》 | 社 区 | 黄 页 | 人 才
移 动专 题SUNIBM微 软微 创精 华Donews人 邮
我的技术中心 
我的分类 我的文档
全部文章 发表文章
专栏管理 使用说明



 RSS 订阅 
最新文档列表
Windows/.NET
.NET  (rss)    
Visual C++  (rss)    
Delphi  (rss)    
Visual Basic  (rss)    
ASP  (rss)    
JavaScript  (rss)    
Java/Linux
Java  (rss)    
Perl  (rss)    
综合
其他开发语言  (rss)    
文件格式  (rss)    
企业开发
游戏开发  (rss)    
网站制作技术  (rss)    
数据库
数据库开发  (rss)    
软件工程
其他  (rss)    

积极原创作者 
Kendiv (114)
btbtd (84)
soft_bird (8)
nizhigang2000 (11)
coofucoo (110)
iyanglian (101)
Aoouch (11)
NinGoo (28)
amh (72)
superyan (27)
CSDN - 文档中心 - 网站制作技术 阅读:545   评论: 0    参与评论
标题   javascript函数库:core.js     选择自 treeroot 的 Blog
关键字   javascript函数库:core.js
出处  

/**
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;
}


相关文章
对该文的评论