javascript 框架小结
/*========================================================================================
框架核心内容--------【基础工具类】
========================================================================================
*/
if(!window.XiaoFeng || !XiaoFeng || typeof XiaoFeng == "undefined")var XiaoFeng = window.XiaoFeng = new Object();
var Prototype = {
Version: "1.4.9",
ScriptFragment: '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)',
emptyFunction: function(){},
K: function(e){return e}
}
/*========================================================================================
【为Object类添加静态方法】
创建于[2008-08-13]
为Object类添加静态方法:extend
*/
Object.extend = function(destination,source){
for(property in source)destination[property] = source[property];
return destination;
}
//通过Object类为每个对象添加方法extend
Object.prototype.extend = function(object){return Object.extend.apply(this,[this,object]);}
/*===========================================================================================
【给数组添加属性】
[创建于2009-06-12]
对数组添加附加属性
*/
Object.extend(Array.prototype,{
remove : function(N){//移除指定的元素
if(isNaN(N)||N>this.length)return false;
for(var i=0,n=0;i<this.length;i++)if(i!=N)this[n++] = this[i];
this.length -= 1;
},
add : function(v){//添加一数组元素
for(var i=0;i<this.length;i++)
if(this[i].toString() == v.toString()) break;
this.push(v);
},
index : function(s){//指定数组中元素的索引
var FlagIndex = -1;
for(var i = 0;i < this.length; i++)
if(this[i].toString() == s.toString()){FlagIndex = i; break;}
return FlagIndex;
},
first : function(){return this[0];},//数组中第一个元素
last : function(){return this[this.length - 1];},//数组中最后一个元素
clear : function(){this.length = 0; return this;}//清空数组中的元素
});
/*===========================================================================================
【给字符串添加属性】
[创建于2009-06-12]
给字符串加附加属性
*/
Object.extend(String.prototype,{
len : function(){return this.replace(/[^\x00-\xff]/g,"ya").length;},//字符串的长度,一个汉字为两个
Length : function(){
var M = 0;
for(var i=0;i<this.length;i++){
if(this.charCodeAt(i) > 255)
M = M + 2;
else
M = M + 1;
}
return M;
},
Trim : function(s){return this.TrimStart(s).TrimEnd(s);},//清空开始与结尾的指定字符
TrimStart : function(s){//清空开始的指定字符
if(!s)s = "\\s+";
var trimStartStr = new RegExp("^("+ s +")+","g");
return this.replace(trimStartStr,"");
},
TrimEnd : function(s){//清空结尾的指定字符
if (!s)s = "\\s+";
var trimEndStr = new RegExp("("+ s +")+$","g");
return this.replace(trimEndStr,"");
},
StartsWith : function(s){
if (!s)s = "\\s";
var startsWithStr = new RegExp("^("+ s +")","g");
return startsWithStr.test(this);
},
EndsWith : function(s){
if (!s)s = "\\s";
var endsWithStr = new RegExp("("+ s +")$","g");
return endsWithStr.test(this);
},
wTh : function(){//全角转换半角
var s = "";
for(var i = 0;i < this.length; i++)
s += this.charCodeAt(i) >= 65248?String.fromCharCode(this.charCodeAt(i) - 65248):this.charAt(i);
return s;
},
hTw : function(){//半角转换全角
var s = "";
for(var i = 0;i < this.length; i++)
s += this.charCodeAt(i) < 65248?String.fromCharCode(this.charCodeAt(i) + 65248):this.charAt(i);
return s;
},
LeftStr : function(M){//左边指定长度字符
if(this.Length() > M){
var str = "";
for(var i=0;i<this.length;i++){
if(this.charCodeAt(i) > 255)
M -= 2;
else
M -= 1;
str += this.substring(i,i+1);
if(M <= 0)break;
}
return str + "...";
}else
r