詳解JS中Array對(duì)象擴(kuò)展與String對(duì)象擴(kuò)展
廢話不多說了,直接給大家上array對(duì)象擴(kuò)展代碼了,具體代碼如下所示:
/** * Created by laixiangran on 2016/01/07. * Array擴(kuò)展 */ (function() { // 遍歷數(shù)組 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); } } }; } // 讓數(shù)組中的每一個(gè)元素調(diào)用給定的函數(shù),然后把得到的結(jié)果放到新數(shù)組中返回 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; }; } // 把符合條件的元素放到一個(gè)新數(shù)組中返回 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; }; } // 如果數(shù)組中的每個(gè)元素都能通過給定的函數(shù)的測(cè)試,則返回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函數(shù),但只要有一個(gè)通過給定函數(shù)的測(cè)試就返回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; }; } // 返回元素在數(shù)組的索引,沒有則返回-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 }; } // 返回元素在數(shù)組的索引,沒有則返回-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; }; } // 讓數(shù)組元素依次調(diào)用給定函數(shù),最后返回一個(gè)值(從左到右) 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; }; } // 讓數(shù)組元素依次調(diào)用給定函數(shù),最后返回一個(gè)值(從右到左) 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; }; } // 去掉重復(fù)項(xiàng)(唯一性),返回新數(shù)組 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; }; } // 指定刪除數(shù)組中某值 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; }; } // 打亂數(shù)組順序 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; }; } // 求數(shù)組的最大值 if (typeof Array.prototype.max != "function") { Array.prototype.max = function() { return Math.max.apply({}, this) }; } // 求數(shù)組的最小值 if (typeof Array.prototype.max != "function") { Array.prototype.min = function() { return Math.min.apply({}, this) }; } // 判斷是否為數(shù)組 if (typeof Array.prototype.isArray != "function") { Array.prototype.isArray = function() { return Object.prototype.toString.apply(this) === "[object Array]"; }; } }());
下面是string對(duì)象擴(kuò)展代碼如下所示:
/** * Created by laixiangran on 2015/12/12. * String擴(kuò)展 */ (function() { // 十六進(jìn)制顏色值的正則表達(dá)式 var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/; // RGB顏色轉(zhuǎn)換為16進(jìn)制 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進(jìn)制顏色轉(zhuǎn)為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, ""); }; } }());
相關(guān)文章
JavaScript實(shí)現(xiàn)滑動(dòng)導(dǎo)航欄效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)滑動(dòng)導(dǎo)航欄效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08點(diǎn)選TOP后并不是直接跳到頁頂?shù)模菨L動(dòng)上去的
點(diǎn)選TOP后并不是直接跳到頁頂?shù)?,而是滾動(dòng)上去的...2007-02-02詳解JavaScript實(shí)現(xiàn)動(dòng)態(tài)的輪播圖效果
這篇文章主要介紹了JavaScript實(shí)現(xiàn)動(dòng)態(tài)的輪播圖效果,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04javascript中l(wèi)ayim之查找好友查找群組
這篇文章主要介紹了javascript中l(wèi)ayim之查找好友查找群組,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02javascript異步編程代碼書寫規(guī)范Promise學(xué)習(xí)筆記
這篇文章主要介紹了javascript異步編程代碼書寫規(guī)范Promise學(xué)習(xí)筆記,需要的朋友可以參考下2015-02-02JavaScript平鋪數(shù)組轉(zhuǎn)樹形結(jié)構(gòu)的實(shí)現(xiàn)示例
本文主要介紹了JavaScript平鋪數(shù)組轉(zhuǎn)樹形結(jié)構(gòu)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07JavaScript ES6中export、import與export default的用法和區(qū)別
這篇文章主要給大家介紹了JavaScript ES6中export、import與export default的用法和區(qū)別,文中介紹的非常詳細(xì),相信對(duì)大家學(xué)習(xí)ES6會(huì)有一定的幫助,需要的朋友可以參考借鑒,下面來一起看看吧。2017-03-03