详解JS中Array对象扩展与String对象扩展


Posted in Javascript onJanuary 07, 2016

废话不多说了,直接给大家上array对象扩展代码了,具体代码如下所示:

/**
* Created by laixiangran on 2016/01/07.
* Array扩展
*/
(function() {
// 遍历数组
if (typeof Array.prototype.forEach != "function") {
Array.prototype.forEach = function (fn, context) {
for (var i = 0; i < this.length; i++) {
if (typeof fn === "function" && Object.prototype.hasOwnProperty.call(this, i)) {
fn.call(context, this[i], i, this);
}
}
};
}
// 让数组中的每一个元素调用给定的函数,然后把得到的结果放到新数组中返回
if (typeof Array.prototype.map != "function") {
Array.prototype.map = function (fn, context) {
var arr = [];
if (typeof fn === "function") {
for (var k = 0, length = this.length; k < length; k++) {
arr.push(fn.call(context, this[k], k, this));
}
}
return arr;
};
}
// 把符合条件的元素放到一个新数组中返回
if (typeof Array.prototype.filter != "function") {
Array.prototype.filter = function (fn, context) {
var arr = [];
if (typeof fn === "function") {
for (var k = 0, length = this.length; k < length; k++) {
fn.call(context, this[k], k, this) && arr.push(this[k]);
}
}
return arr;
};
}
// 如果数组中的每个元素都能通过给定的函数的测试,则返回true,反之false
if (typeof Array.prototype.every != "function") {
Array.prototype.every = function (fn, context) {
var passed = true;
if (typeof fn === "function") {
for (var k = 0, length = this.length; k < length; k++) {
if (passed === false) break;
passed = !!fn.call(context, this[k], k, this);
}
}
return passed;
};
}
// 类似every函数,但只要有一个通过给定函数的测试就返回true
if (typeof Array.prototype.some != "function") {
Array.prototype.some = function (fn, context) {
var passed = false;
if (typeof fn === "function") {
for (var k = 0, length = this.length; k < length; k++) {
if (passed === true) break;
passed = !!fn.call(context, this[k], k, this);
}
}
return passed;
};
}
// 返回元素在数组的索引,没有则返回-1,从左到右
if (typeof Array.prototype.indexOf != "function") {
Array.prototype.indexOf = function (item, index) {
var n = this.length,
i = index == null ? 0 : index < 0 ? Math.max(0, n + index) : index;
for (; i < n; i++) {
if (i in this && this[i] === item) {
return i
}
}
return -1
};
}
// 返回元素在数组的索引,没有则返回-1,从右到左
if (typeof Array.prototype.lastIndexOf != "function") {
Array.prototype.lastIndexOf = function (item, index) {
var n = this.length,
i = index == null ? n-1 : index < 0 ? Math.max(0, n + index) : index;
for (; i >= 0; i--) {
if (i in this && this[i] === item) {
return i;
}
}
return -1;
};
}
// 让数组元素依次调用给定函数,最后返回一个值(从左到右)
if (typeof Array.prototype.reduce != "function") {
Array.prototype.reduce = function (callback, initialValue) {
var previous = initialValue, k = 0, length = this.length;
if (typeof initialValue === "undefined") {
previous = this[0];
k = 1;
}
if (typeof callback === "function") {
for (k; k < length; k++) {
this.hasOwnProperty(k) && (previous = callback(previous, this[k], k, this));
}
}
return previous;
};
}
// 让数组元素依次调用给定函数,最后返回一个值(从右到左)
if (typeof Array.prototype.reduceRight != "function") {
Array.prototype.reduceRight = function (callback, initialValue) {
var length = this.length, k = length - 1, previous = initialValue;
if (typeof initialValue === "undefined") {
previous = this[length - 1];
k--;
}
if (typeof callback === "function") {
for (k; k > -1; k-=1) {
this.hasOwnProperty(k) && (previous = callback(previous, this[k], k, this));
}
}
return previous;
};
}
// 去掉重复项(唯一性),返回新数组
if (typeof Array.prototype.uniq != "function") {
Array.prototype.uniq = function() {
var arr = [];
arr[0] = this[0];
for (var i = 1; i < this.length; i++) {
if (arr.indexOf(this[i]) == -1) {
arr.push(this[i]);
}
}
return arr;
};
}
// 指定删除数组中某值
if (typeof Array.prototype.remove != "function") {
Array.prototype.remove = function(item) {
for (var i = this.length; i >= 0; i--) {
if (item === this[i]) {
this.splice(i, 1);
}
}
return this;
};
}
// 打乱数组顺序
if (typeof Array.prototype.shuffle != "function") {
Array.prototype.shuffle = function() {
var i = this.length;
while (i) {
var j = Math.floor(Math.random()*i);
var t = this[--i];
this[i] = this[j];
this[j] = t;
}
return this;
};
}
// 求数组的最大值
if (typeof Array.prototype.max != "function") {
Array.prototype.max = function() {
return Math.max.apply({}, this)
};
}
// 求数组的最小值
if (typeof Array.prototype.max != "function") {
Array.prototype.min = function() {
return Math.min.apply({}, this)
};
} 

// 判断是否为数组
if (typeof Array.prototype.isArray != "function") {
Array.prototype.isArray = function() {
return Object.prototype.toString.apply(this) === "[object Array]";
};
} 
}());

下面是string对象扩展代码如下所示:

/**
* Created by laixiangran on 2015/12/12.
* String扩展
*/
(function() {
// 十六进制颜色值的正则表达式
var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
// RGB颜色转换为16进制
if (typeof String.prototype.rgbToHex != "function") {
String.prototype.rgbToHex = function() {
var that = this;
if (/^(rgb|RGB)/.test(that)) {
var aColor = that.replace(/(?:\(|\)|rgb|RGB)*/g,"").split(",");
var strHex = "#";
for (var i=0; i<aColor.length; i++) {
var hex = Number(aColor[i]).toString(16);
if (hex === "0") {
hex += hex;
}
strHex += hex;
}
if (strHex.length !== 7) {
strHex = that;
}
return strHex;
}else if (reg.test(that)) {
var aNum = that.replace(/#/,"").split("");
if (aNum.length === 6){
return that;
}else if (aNum.length === 3) {
var numHex = "#";
for (var j=0; j<aNum.length; j++) {
numHex += (aNum[j]+aNum[j]);
}
return numHex;
}
}else{
return that;
}
};
}
// 16进制颜色转为RGB格式
if (typeof String.prototype.hexToRgb != "function") {
String.prototype.hexToRgb = function() {
var sColor = this.toLowerCase();
if (sColor && reg.test(sColor)) {
if (sColor.length === 4) {
var sColorNew = "#";
for (var i = 1; i < 4; i++) {
sColorNew += sColor.slice(i,i+1).concat(sColor.slice(i,i+1));
}
sColor = sColorNew;
}
// 处理六位的颜色值
var sColorChange = [];
for (var j=1; j<7; j+=2) {
sColorChange.push(parseInt("0x"+sColor.slice(j,j+2)));
}
return "RGB(" + sColorChange.join(",") + ")";
}else{
return sColor;
}
};
}
// 移除字符串首尾空白
if (typeof String.prototype.trim != "function") {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, "");
};
}
}());
Javascript 相关文章推荐
window.addeventjs事件驱动函数集合addEvent等
Feb 19 Javascript
JSQL 基于客户端的成绩统计实现方法
May 05 Javascript
js 获取、清空input type=&quot;file&quot;的值示例代码
Feb 19 Javascript
jQuery中:visible选择器用法实例
Dec 30 Javascript
分享javascript计算时间差的示例代码
Mar 19 Javascript
在IE8上JS实现combobox支持拼音检索功能
May 23 Javascript
浅析Bootstrap表格的使用
Jun 23 Javascript
JavaScript实现经典排序算法之插入排序
Dec 28 Javascript
浅析vue数据绑定
Jan 17 Javascript
vue实现手机号码抽奖上下滚动动画示例
Oct 18 Javascript
如何基于js判断浏览器版本
Feb 20 Javascript
使用纯前端JavaScript实现Excel导入导出方法过程详解
Aug 07 Javascript
js创建对象的方法汇总
Jan 07 #Javascript
JavaScript截取、切割字符串的技巧
Jan 07 #Javascript
js确认框confirm()用法实例详解
Jan 07 #Javascript
实例讲解jquery与json的结合
Jan 07 #Javascript
javascript仿百度输入框提示自动下拉补全
Jan 07 #Javascript
基于jquery实现表格无刷新分页
Jan 07 #Javascript
js密码强度检测
Jan 07 #Javascript
You might like
ThinkPHP2.0读取MSSQL提示Incorrect syntax near the keyword 'AS'的解决方法
2014/06/25 PHP
完美实现wordpress禁止文章修订和自动保存的方法
2014/11/03 PHP
PHP动态编译出现Cannot find autoconf的解决方法
2014/11/05 PHP
PHP符合PSR编程规范的实例分享
2016/12/21 PHP
php 数据结构之链表队列
2017/10/17 PHP
用一段js程序来实现动画功能
2007/03/06 Javascript
北京奥运官方网站幻灯切换效果flash版打包下载
2008/01/30 Javascript
jquery使用淘宝接口跨域查询手机号码归属地实例
2013/11/28 Javascript
JS如何将数字类型转化为没3个一个逗号的金钱格式
2014/01/27 Javascript
jQuery Ajax使用实例
2015/04/16 Javascript
javascript仿百度输入框提示自动下拉补全
2016/01/07 Javascript
Vue绑定内联样式问题
2018/10/17 Javascript
vue iview多张图片大图预览、缩放翻转
2019/07/13 Javascript
vue按需加载实例详解
2019/09/06 Javascript
ElementUI多个子组件表单的校验管理实现
2019/11/07 Javascript
微信小程序关键字变色实现代码实例
2019/12/13 Javascript
Vue常用的全选/反选的示例代码
2020/02/19 Javascript
浅谈vue获得后台数据无法显示到table上面的坑
2020/08/13 Javascript
JavaScript实现下拉列表
2021/01/20 Javascript
python多线程编程中的join函数使用心得
2014/09/02 Python
跟老齐学Python之类的细节
2014/10/13 Python
使用Python求解最大公约数的实现方法
2015/08/20 Python
python matplotlib画图实例代码分享
2017/12/27 Python
python实现遍历文件夹修改文件后缀
2018/08/28 Python
Python 多线程不加锁分块读取文件的方法
2018/12/11 Python
用Python解数独的方法示例
2019/10/24 Python
利用python实现冒泡排序算法实例代码
2019/12/01 Python
python 解决flask 图片在线浏览或者直接下载的问题
2020/01/09 Python
PyQt5连接MySQL及QMYSQL driver not loaded错误解决
2020/04/29 Python
Python csv文件记录流程代码解析
2020/07/16 Python
Python爬虫简单运用爬取代理IP的实现
2020/12/01 Python
微软俄罗斯官方网站:Microsoft俄罗斯
2016/09/18 全球购物
Ever New加拿大官网:彰显女性美
2018/10/05 全球购物
六查六看心得体会
2014/10/14 职场文书
机关班子查摆问题及整改措施
2014/10/28 职场文书
合作意向书范本
2019/04/17 职场文书