prototype 属性重写对象方法和新定义对象方法

<script>
/**
* @From: http://www.happyshow.org/article.asp?id=135
* @From: http://www.blueidea.com/tech/web/2003/1402.asp
* @From: http://www.cnscn.org
*/

/**
*  具有prototype属性的类型
*
* 数组变量(Array)
* 逻辑变量(Boolean)
* 日期变量(Date)
* 结构变量(Function)
* 数值变量(Number)
* 对象变量(Object)
* 字符串变量(String)
*/


/**
* 去掉字符串首尾的空格
*/
String.prototype.cnsTrim = function(){
    var reg=/(^\s*)|(\s*$)/g;
    return this.replace(reg, "");
}

/**
* 判断是否合法email
*/
String.prototype.cnsValidEmail = function() {
   var reg=/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
   return reg.test(this);
}

/**
* 判断是否数字字符串
*/
String.prototype.cnsIsDigit = function() {
   var reg=/^([+-])?\d*\.?\d*$/;
   return reg.test(this);
}

/**
* 判断是否数字
*/
Number.prototype.cnsIsDigit = function() {
   var reg=/^([+-])?\d*\.?\d*$/;
   return reg.test(this);
}

 

/**
* 兼容汉字取字符串字节长
*/
String.prototype.cnsLength = function(){
   var arr=this.match(/[^\x00-\xff]/ig);
   return this.length+(arr==null?0:arr.length);
}

/**
* 截取字符串的左侧子串
*/
String.prototype.left = function(num,mode){
        if(!/\d+/.test(num))return(this);
        var str = this.substr(0,num);
        if(!mode) return str;
        var n = str.cnsLength() - str.length;
        num = num - parseInt(n/2);
        return this.substr(0,num);
    }


/**
* 取%或千分比等
*/
Number.prototype.toFixed = function(n) {
        with(Math) return round(Number(this)*pow(10,n))/pow(10,n)
    }

/**
* 取出数组元素
*/
Array.prototype.pop = function() {
        var lastElement = this[this.length-1];
        this.length = Math.max(this.length-1,0);
        return lastElement;
    }

/**
* 把元素加入数组
*/
Array.prototype.push = function(new_element){
   this[this.length]=new_element;
   return this.length;
}

/**
* 把多个元素都加入数组
*/
Array.prototype.push = function() {
        for(var i=0;i<arguments.length;i++) {
            this[this.length]=arguments;
        }
        return this.length;
    }


/**
* 删除一个元素
*/
Array.prototype.shift = function() {
        var firstElement = this[0];
        this.reverse();
        this.pop();
        this.reverse();
        return firstElement;
}

Array.prototype.unshift = function() {
        var arr = new Array();
        for (var i=0;i<arguments.length;arr=arguments[i++]);
        arr = arr.concat(this);
        this.length = 0;
        for (i=0;i<arr.length;this=arr[i++]);
}


/**
* 对数组进行切片
*/
Array.prototype.splice = function() {
        var start = arguments[0];
        var deleteCount = start+arguments[1];
        var deleteItem = this.slice(start,deleteCount);
        var beforeItem = this.slice(0,start);
        var afterItem = this.slice(deleteCount);
        this.length=beforeItem.length;
        var i;
        for (i=2;i<arguments.length;this[this.length]=arguments[i++]);
        for (i=0;i<afterItem.length;this[this.length]=afterItem[i++]);
        return deleteItem;
}


/**
* 过滤字符串里面的html标记,类似于 innerText
*/
String.prototype.stripTags = function() {
    return this.replace(/<\/?[^>]+>/gi, '');
}

/**
*  过滤掉字符串里面的js
*/
String.prototype.stripScripts = function() {
    return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), '');
}

/**
* 与 stripScript 相反,取出script标签里面的代码,不包括 <script>标记本身
*/
String.prototype.extractScripts = function() {
    var matchAll = new RegExp(Prototype.ScriptFragment, 'img');
    var matchOne = new RegExp(Prototype.ScriptFragment, 'im');
    return (this.match(matchAll) || []).map(function(scriptTag) {
      return (scriptTag.match(matchOne) || ['', ''])[1];
    });
}

/**
* 运行脚本
*/
String.prototype.evalScripts = function() {
    return this.extractScripts().map(eval);
  }

/**
* 类似 escape,转换 < > 为 < >
*/
String.prototype.escapeHTML = function() {
    var div = document.createElement('div');
    var text = document.createTextNode(this);
    div.appendChild(text);
    return div.innerHTML;
}

/**
* 和上面相反
*/
String.prototype.unescapeHTML = function() {
    var div = document.createElement('div');
    div.innerHTML = this.stripTags();
    return div.childNodes[0] ? div.childNodes[0].nodeValue : '';
}


/**
* var stringA = "?id=2&type=product"; 
* stringA.toQueryParams() 将返回 {id="2"; type="product"}
* 这是个object,具有id与type属性。
*/
String.prototype.toQueryParams = function() {
    var pairs = this.match(/^\??(.*)$/)[1].split('&');
    return pairs.inject({}, function(params, pairString) {
      var pair = pairString.split('=');
      params[pair[0]] = pair[1];
      return params;
    });
}

/**
* 把字符串转换为数组,每个字符占一个数组元素
*/
String.prototype.toArray = function(arg) {
    return this.split(arg?arg:' ');
}


/**
* 把用减号连接的组合单词如:background-color连接成 backgroundColor
* 调用方法:var ss ="background_color"; ss.camelize();
*/
String.prototype.camelize = function() {
    var oStringList = this.split('-');
    if (oStringList.length == 1) return oStringList[0];

    var camelizedString = this.indexOf('-') == 0
      ? oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1)
      : oStringList[0];

    for (var i = 1, len = oStringList.length; i < len; i++) {
      var s = oStringList;
      camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
    }

    return camelizedString;
}


/**
* 转义单引号与斜杠
*/
String.prototype.inspect = function() {
    return "'" + this.replace('\\', '\\\\').replace("'", '\\\'') + "'";
}


//测试
//1)
var str="     xxxxxxxxx   ";
alert("'"+str.cnsTrim()+"'");


//2)
var str="cnscn@163.com";
alert(str.cnsValidEmail());

//3)
var str="-456"; //调用String.prototype.cnsIsDigit()
alert(str.cnsIsDigit());

var str=-456; //调用Number.prototype.cnsIsDigit()
alert(str.cnsIsDigit());


var arr = new Array();
arr.push(1);
arr.push(2);
arr.push(3);
alert(arr);


var str=" 兼\"容";
//alert(str.cnsLength());

//alert(str.left(2, true));
alert(str.toArray("'"));
</script>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值