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;
    }
  • 相关阅读:
    9.1 正则介绍_grep上 9.2 grep中 9.3 grep下
    汉诺塔递归问题
    8.10 shell特殊符号cut命令 8.11 sort_wc_uniq命令 8.12 tee_tr_split命令 8.13 shell特殊符号下
    8.6 管道符和作业控制 8.7/8.8 shell变量 8.9 环境变量配置文件
    8.1 shell介绍 8.2 命令历史 8.3 命令补全和别名 8.4 通配符 8.5 输入输出重定向
    socket中使用序列化传结构体
    C# get,set属性用法
    Log4Net如何将日志按不同类型写入多个文件中
    app.config文件的configation标签中加代码引起”配置系统未能初始化“
    WinForm下使用 log4net
  • 原文地址:https://www.cnblogs.com/jiechen/p/5463913.html
Copyright © 2011-2022 走看看