📄 extend.js
字号:
/*object inherit*/
Object.extend = function(target, source, replace) {
for(var prop in source) {
if(replace == false && target[prop] != null) { continue; }
target[prop] = source[prop];
}
return target;
};
//Object.extend(
// Object.prototype,{
// inheritFrom : function (func) {
// if (this.constructor == func) {
// return true;
// } else if (typeof this.constructor.__superclasses__ == "object") {
// for (var i=0; i < this.constructor.__superclasses__.length; i++) {
// if (this.constructor.__superclasses__[i] == func) {
// return true;
// }
// }
// return false;
// } else {
// return false;
// }
// },
// instanceOf : function (func) {
// if (this.constructor == func) {
// return true;
// } else if (typeof this.constructor.__superclasses__ == "object") {
// for (var i=0; i < this.constructor.__superclasses__.length; i++) {
// if (this.constructor.__superclasses__[i] == func) {
// return true;
// }
// }
// return false;
// } else {
// return false;
// }
// }
// },false);
// String Extend Methods
Object.extend(
String.prototype,{
trimStart : function(){
var tmp = this;
return tmp.replace(/^\s*/g,"");
},
trimEnd : function(){
var tmp = this;
return tmp.replace(/\s*$/g,"");
},
trim : function(){
var tmp= this;
return tmp.trimStart().trimEnd();
},
startWith : function(prefix){
return (this.substr(0, prefix.length) == prefix);
},
endWith : function(suffix){
return (this.substr(this.length - suffix.length) == suffix);
}
},false);
Object.extend(
String,{
format : function(){
if(arguments.length>1){
var tmpFormat = arguments[0],tmpArg = [];
if(arguments[1] instanceof Array){
tmpArg = arguments[1]
}else{
tmpArg = [].slice.call(arguments).slice(1);
}
for(var i=0;i<tmpArg.length;i++){
tmpFormat = tmpFormat.replace("{"+i+"}",tmpArg[i]);
}
return tmpFormat;
}
return arguments[0] || "";
}
},false);
function TrimStr(str){
if(str == undefined || str == null)
return "";
return str.trim();
}
/*Array Extend Methods*/
Object.extend(
Array.prototype,{
splice : function () {// -- for ie 5 splice
var start = arguments[0],deleteCount = arguments[1];
var len = arguments.length - 2;
var returnValue = this.slice(start);
for (var i = 0; i < len; i++) {
this[start + i] = arguments[i + 2];
}
for (var i = 0; i < returnValue.length - deleteCount; i++) {
this[start + len + i] = returnValue[deleteCount + i];
}
this.length = start + len + returnValue.length - deleteCount;
returnValue.length = deleteCount;
return returnValue;
},
push : function(new_ele){
this[this.length] = new_ele;
return this.length;
},
contains : function(value){
var exist = false; for(var i=0;i<this.length;i++){ if(this[i] == value){ exist = true; break; } } return exist;
},
add : function(value){
if(!this.contains(value)){ this.push(value);
}
},
addRange : function (items){
var length = items.length;
if (length != 0) {
for (var index = 0; index < length; index++) {
this.push(items[index]);
}
}
},
remove : function(value){
var len = this.length;
for(var i=0;i<len;i++){
if(this[i] == value){
this.splice(i, 1);
break;
}
}
},
removeAt : function(index){
this.splice(index, 1);
},
clear : function(){
if (this.length > 0) {
this.splice(0, this.length);
}
},
clone : function(){
var clonedArray = [];
var length = this.length;
for (var index = 0; index < length; index++) {
clonedArray[index] = this[index];
}
return clonedArray;
},
indexOf : function(item){
var length = this.length;
if (length != 0) {
for (var index = 0; index < length; index++) {
if (this[index] == item) {
return index;
}
}
}
return -1;
},
insert : function(index,item){
this.splice(index, 0, item);
}
},false);
function removeArrItem(arrs,item){
var tempArr = [] ;
for(var arr in arrs){
if(arrs[arr] != item){
tempArr.push(arrs[arr]);
}
}
return tempArr;
}
/*Number Extend Methods*/
Object.extend(
Number.prototype,{
NaN0 : function(){return isNaN(this)?0:this;}
},false);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -