扩展原生js的Array类

     Array.prototype.add = function(item){
this.push(item);
}
Array.prototype.addRange = function(items){
var length = items.length;
if(length!=0){
for (var index = 0; index < length; index++) {
this.push(items[index]); }
}
}
Array.prototype.clear = function(){
if(this.length>0){
this.splice(0,this.length);
}
}
Array.prototype.isEmpty = function(){
if(this.length == 0){
return true;
}
else{
return false;
}
}
Array.prototype.clone = function(){
var clonedArray = [];
var length = this.length;
for (var index = 0; index < length; index++) {
clonedArray[index] = this[index];
}
return clonedArray;
}
Array.prototype.contains = function(item){
var index = this.indexOf(item);
return (index>=0);
}
Array.prototype.dequeue = function(){
return this.shift();
}
Array.prototype.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;
}
Array.prototype.insert = function(index,item){
this.splice(index,0,item);
}
Array.prototype.joinstr = function(str){
var newStr = new Array(this.length);
for (var i = 0; i < this.length; i++) {
newStr[i] = this[i]+str;
}
return newStr;
}
Array.prototype.queue = function(item){//入队
this.push(item);
}
Array.prototype.remove = function(item){
var index = this.indexOf(item);
if(index >= 0){
this.splice(index,1);
}
}
Array.prototype.removeAt = function(index){
this.splice(index,1);
}
//给js原生Array增加each方法
Array.prototype.each = function(fn)
{
return this.length ? [fn(this.slice(0,1))].concat(this.slice(1).each(fn)) : [];
}; [1,2,3,4].each(function(x){
document.write(x + "<br/>");
});

原生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;
}

最新文章

  1. CSS布局 ——从display,position, float属性谈起
  2. Flatic – 超齐全的 Web 元素界面素材库免费下载
  3. iOS图像资源Images Assets
  4. javascript预解析和作用域
  5. php 仿百度文库
  6. hdu1286 寻找新朋友 (欧拉功能)
  7. Document Classification
  8. python中xrange用法分析
  9. Linux通过shell执行自动化部署
  10. brew的MAC安装
  11. 基于struts2、hibernate、spring、shiro、MySQL的项目开发
  12. 爬虫模块介绍--Beautifulsoup (解析库模块,正则)
  13. 控制页面打印的2种方法(css3的media媒体查询和window.print())
  14. MySQL 从库down机
  15. AngularJS + CoffeeScript 前端开发环境配置详解
  16. State of Serverless
  17. 分类Category的概念和使用流程
  18. Statement和PreparedStatement之间的区别
  19. noise_process.c
  20. 170512、java日志文件log4j.properties配置详解

热门文章

  1. jquerymobile知识点:button与a
  2. winform中如何在TextBox中只能输入数字(可以带小数点)
  3. Chrome/Chromium HTML5 video 视频播放硬件加速
  4. LINUX 文件系统JBD ----深入理解Fsync
  5. WebService学习笔记系列(四)
  6. 使用泛型定义一个可重用的Dao
  7. Base64原理简介
  8. 服務器提交協議衝突 (The server committed a protocol violation.)
  9. viewpager在最后一页滑动之后,跳转到主页面
  10. Bootstrap: 样式CSS:carousel轮换 图片的使用