详解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 相关文章推荐
JavaScript DOM 学习第九章 选取范围的介绍
Feb 19 Javascript
基于jQuery的计算文本框字数的代码
Jun 06 Javascript
jQuery滚动加载图片效果的实现
Mar 06 Javascript
js完美的div拖拽实例代码
Jan 22 Javascript
javascript实现的HashMap类代码
Jun 27 Javascript
Javascript学习笔记之函数篇(四):arguments 对象
Nov 23 Javascript
EasyUI折叠表格层次显示detailview详解及实例
Dec 28 Javascript
Angularjs 1.3 中的$parse实例代码
Sep 14 Javascript
JavaScript使用闭包模仿块级作用域操作示例
Jan 21 Javascript
JavaScript ES6 Class类实现原理详解
May 08 Javascript
解决vue里a标签值解析变量,跳转页面,前面加默认域名端口的问题
Jul 22 Javascript
js获取url页面id,也就是最后的数字文件名
Sep 25 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
Protoss热键控制
2020/03/14 星际争霸
php面向对象全攻略 (三)特殊的引用“$this”的使用
2009/09/30 PHP
php实现通用的信用卡验证类
2015/03/24 PHP
PHP简单字符串过滤方法示例
2016/09/04 PHP
jQuery中与toggleClass等价的程序段 以及未来学习的方向
2010/03/18 Javascript
jquery创建并行对象或者合并对象的实现代码
2012/10/10 Javascript
JS获取网页属性包括宽、高等等
2014/04/03 Javascript
基于jQuery实现的向下滑动二级菜单效果代码
2015/08/31 Javascript
javascript实现下拉提示选择框
2015/12/29 Javascript
javascript操作cookie
2017/01/17 Javascript
微信小程序中input标签详解及简单实例
2017/05/18 Javascript
vue内置指令详解
2018/04/03 Javascript
jQuery实现判断上传图片类型和大小的方法示例
2018/04/11 jQuery
js回文数的4种判断方法示例
2019/06/04 Javascript
Vue初始化中的选项合并之initInternalComponent详解
2020/06/11 Javascript
Element图表初始大小及窗口自适应实现
2020/07/10 Javascript
[01:02:34]TFT vs VGJ.T Supermajor 败者组 BO3 第二场 6.5
2018/06/06 DOTA
Python实现XML文件解析的示例代码
2018/02/05 Python
TensorFlow模型保存/载入的两种方法
2018/03/08 Python
Python爬虫抓取代理IP并检验可用性的实例
2018/05/07 Python
tensorflow saver 保存和恢复指定 tensor的实例讲解
2018/07/26 Python
Python对ElasticSearch获取数据及操作
2019/04/24 Python
Python中@property的理解和使用示例
2019/06/11 Python
Flask框架模板渲染操作简单示例
2019/07/31 Python
python pillow模块使用方法详解
2019/08/30 Python
将python安装信息加入注册表的示例
2019/11/20 Python
Python使用Pyqt5实现简易浏览器(最新版本测试过)
2020/04/27 Python
如何在python中实现线性回归
2020/08/10 Python
CSS3的first-child选择器实战攻略
2016/04/28 HTML / CSS
中介业务员岗位职责
2014/04/09 职场文书
综治宣传月活动总结
2014/04/28 职场文书
给市场的环保建议书
2014/05/14 职场文书
个人剖析材料及整改措施
2014/10/07 职场文书
骨干教师考核评语
2014/12/31 职场文书
2015年乡镇残联工作总结
2015/05/13 职场文书
mysql5.7使用binlog 恢复数据的方法
2021/06/03 MySQL