这篇文章主要介绍了详解js中array对象扩展与string对象扩展的相关资料,需要的朋友可以参考下
废话不多说了,直接给大家上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, "");
};
}
}());
以上就是详解js中array对象扩展与string对象扩展的方法的详细内容。
