zoukankan      html  css  js  c++  java
  • 扩展原生js的一些方法

    扩展原生js的Array类

     1     Array.prototype.add = function(item){
     2         this.push(item);
     3     }
     4     Array.prototype.addRange = function(items){
     5         var length = items.length;
     6         if(length!=0){
     7             for (var index = 0; index < length; index++) {
     8                 this.push(items[index]);
     9                 
    10             }
    11         }
    12     }
    13     Array.prototype.clear = function(){
    14         if(this.length>0){
    15             this.splice(0,this.length);
    16         }
    17     }
    18     Array.prototype.isEmpty = function(){
    19         if(this.length == 0){
    20             return true;
    21         }
    22         else{
    23             return false;
    24         }
    25     }
    26     Array.prototype.clone = function(){
    27         var clonedArray = [];
    28         var length = this.length;
    29         for (var index = 0; index < length; index++) {
    30             clonedArray[index] = this[index];
    31         }
    32         return clonedArray;
    33     }
    34     Array.prototype.contains = function(item){
    35         var index = this.indexOf(item);
    36         return (index>=0);
    37     }
    38     Array.prototype.dequeue = function(){
    39         return this.shift();
    40     }
    41     Array.prototype.indexOf = function(item){
    42         var length = this.length;
    43 
    44         if(length!=0){
    45             for (var index = 0; index < length; index++) {
    46                 if(this[index] == item){
    47                     return index;
    48                 }
    49             }
    50         }
    51         return -1;
    52     }
    53     Array.prototype.insert = function(index,item){
    54         this.splice(index,0,item);
    55     }
    56     Array.prototype.joinstr = function(str){
    57         var newStr = new Array(this.length);
    58         for (var i = 0; i < this.length; i++) {
    59             newStr[i] = this[i]+str;
    60         }
    61         return newStr;
    62     }
    63     Array.prototype.queue = function(item){//入队
    64         this.push(item);
    65     }
    66     Array.prototype.remove = function(item){
    67         var index = this.indexOf(item);
    68         if(index >= 0){
    69             this.splice(index,1);
    70         }
    71     }
    72     Array.prototype.removeAt = function(index){
    73         this.splice(index,1);
    74     }
    75     //给js原生Array增加each方法
    76     Array.prototype.each = function(fn)
    77     {
    78         return this.length ? [fn(this.slice(0,1))].concat(this.slice(1).each(fn)) : [];
    79     };
    80     
    81     [1,2,3,4].each(function(x){
    82         document.write(x + "<br/>");
    83     });

    原生js的String类扩展

        //获取字符数组 
        String.prototype.toCharArray = function(){
            return this.split("");
        } 
        //获取N个相同的字符串 
        String.prototype.repeat = function(num){
            var tmpArr = [];
            for (var i = 0; i < num; i++) {
                temArr.push(this);
                return temArr.join("");
            }
    
        }
        //逆序
        String.prototype.reverse = function(){
            return this.split("").reverse().join("");
    
        }
        //测试是否是数字 
    String.prototype.isNumeric = function() {
        var tmpFloat = parseFloat(this);
        if (isNaN(tmpFloat))
            return false;
        var tmpLen = this.length - tmpFloat.toString().length;
        return tmpFloat + "0".Repeat(tmpLen) == this;
    }
    //测试是否是整数 
    String.prototype.isInt = function() {
        if (this == "NaN")
            return false;
        return this == parseInt(this).toString();
    }
    // 合并多个空白为一个空白 
    String.prototype.resetBlank = function() {
        return this.replace(/s+/g, " ");
    }
    // 除去左边空白 
    String.prototype.LTrim = function() {
        return this.replace(/^s+/g, "");
    }
    // 除去右边空白 
    String.prototype.RTrim = function() {
        return this.replace(/s+$/g, "");
    }
    // 除去两边空白 
    String.prototype.trim = function() {
        return this.replace(/(^s+)|(s+$)/g, "");
    }
    // 保留数字 
    String.prototype.getNum = function() {
        return this.replace(/[^d]/g, "");
    }
    // 保留字母 
    String.prototype.getEn = function() {
        return this.replace(/[^A-Za-z]/g, "");
    }
    // 保留中文 
    String.prototype.getCn = function() {
        return this.replace(/[^u4e00-u9fa5uf900-ufa2d]/g, "");
    }
    // 得到字节长度 
    String.prototype.getRealLength = function() {
        return this.replace(/[^x00-xff]/g, "--").length;
    }
    // 从左截取指定长度的字串 
    String.prototype.left = function(n) {
        return this.slice(0, n);
    }
    // 从右截取指定长度的字串 
    String.prototype.right = function(n) {
        return this.slice(this.length - n);
    }
    // HTML编码 
    String.prototype.HTMLEncode = function() {
        var re = this;
        var q1 = [ /x26/g, /x3C/g, /x3E/g, /x20/g ];
        var q2 = [ "&", "<", ">", " " ];
        for ( var i = 0; i < q1.length; i++)
            re = re.replace(q1[i], q2[i]);
        return re;
    }
    // Unicode转化 
    String.prototype.ascW = function() {
        var strText = "";
        for ( var i = 0; i < this.length; i++)
            strText += "&#" + this.charCodeAt(i) + ";";
        return strText;
    }
  • 相关阅读:
    关于数组的排序和拷贝!
    append和appendTo的区别!
    Layui的一点小理解(上)
    winfrom数据导出
    检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配
    文件监听
    C# winform 启动外部程序
    bat命令复制文件
    3.将模型添加到 ASP.NET Core MVC 应用
    2.将视图添加到 ASP.NET Core MVC 应用
  • 原文地址:https://www.cnblogs.com/jiechen/p/5463913.html
Copyright © 2011-2022 走看看