1. 程式人生 > >js 中prototype運用(陣列)

js 中prototype運用(陣列)


 /* 
  *  方法:Array.removeAt(Index) 
  *  功能:刪除陣列元素. 
  *  引數:Index刪除元素的下標. 
  *  返回:在原陣列上修改陣列 
  */ 
  

Array.prototype.removeAt = function(Index) {
    if (isNaN(Index) || Index > this.length) {
        return false; }
    for (var i = 0, n = 0; i < this.length; i++) {
        if (this[i] != this[Index]) {
            this[n++] = this[i]
        }
    }
    this.length -= 1
}

  /*                             
  *  方法:Array.remove(obj)      
  *  功能:刪除陣列元素.         

  *  引數:要刪除的物件.     
  *  返回:在原陣列上修改陣列    
  */   

Array.prototype.remove = function(obj) {
     if (null == obj) {
         return; }
     for (var i = 0, n = 0; i < this.length; i++) {
         if (this[i] != obj) {
             this[n++] = this[i];
         }
     }
     this.length -= 1
 }

 

 /*                             
  *  方法:Array.Contains(obj)      
  *  功能:確定某個元素是否在陣列中.         
  *  引數:要查詢的Object物件 
  *  返回:找到返回true,否則返回false; 
  */    

Array.prototype.Contains = function(obj) {
     if (null == obj) {
         return; }
     for (var i = 0, n = 0; i < this.length; i++) {
         if (this[i] != obj) {
             return true;
         }
     }

     return false;
 }

 

var arrContains = ['1', '22', '3333', '22'];
console.log(arrContains.Contains('22')); //true

 /*                             
  *  方法:Array.IndexOf(obj)      
  *  功能:搜尋指定的Object,並返回第一個匹配項從零開始的索引         
  *  引數:要查詢的Object物件    
  *  返回:找到返回該元素在陣列中的索引,否則返回-1 
  */  

Array.prototype.IndexOf = function(obj) {
     if (null == obj) {
         return; } {
         for (var i = 0, n = 0; i < this.length; i++) {
             if (this[i] == obj) {
                 return i;
             }
         }
     }

     return -1;
 }

 

 /*                             
  *  方法:Array.Clear()      
  *  功能:消空陣列元素.         
  *  引數:無.     
  *  返回:空陣列 
  */ 

Array.prototype.Clear = function() {
     this.length = 0;
 }

 /*                             
  *  方法:Array.formatString()      
  *  功能:處理陣列為字串.         
  *  引數:無.     
  *  返回:字串
  */ 

Array.prototype.formatString = function() {
    var str = '';
    for (var i = 0; i < this.length; i++) {
        if(i == this.length -1){
            str += this[i];
        }else {
            str += this[i] + ';';
        }
    }
    return str;
};

  

 

$.prototype.serializeObject = function() {
    var a, o, h, i, e;
    a = this.serializeArray();
    o = {};
    h = o.hasOwnProperty;
    for (i = 0; i < a.length; i++) {
        e = a[i];
        if (!h.call(o, e.name)) {
            o[e.name] = e.value;
        }
    }
    return o;
};
$.fn.serializeJson = function() {
    var arr = this.serializeArray();
    var json = {};
    arr.forEach(function(item) {
        var name = item.name;
        var value = item.value;

        if (!json[name]) {
            json[name] = value;
        } else if ($.isArray(json[name])) {
            json[name].push(value);
        } else {
            json[name] = [json[name], value];
        }
    });
    return json;
}

 //日期處理

Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //
        "h+": this.getHours(), //小時
        "m+": this.getMinutes(), //
        "s+": this.getSeconds(), //
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

 


更多專業前端知識,請上 【猿204