首页 - 信息 - 详解JS中Array对象扩展与String对象扩展【javascript】

详解JS中Array对象扩展与String对象扩展【javascript】

2023-09-21 11:33
web前端|js教程
js_array扩展,js_string扩展
web前端-js教程
废话不多说了,直接给大家上array对象扩展代码了,具体代码如下所示:
科技公司源码,ubuntu鼠标右键选项,爬虫html取消换行,amazeui PHP,查询seo优化lzw
/*** 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" && www.gsm-guard.net(this, i)) {www.gsm-guard.net(context, this[i], i, this);}}};}// 让数组中的每一个元素调用给定的函数,然后把得到的结果放到新数组中返回if (typeof www.gsm-guard.net != "function") {www.gsm-guard.net = function (fn, context) {var arr = [];if (typeof fn === "function") {for (var k = 0, length = this.length; k < length; k++) {arr.push(www.gsm-guard.net(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++) {www.gsm-guard.net(context, this[k], k, this) && arr.push(this[k]);}}return arr;};}// 如果数组中的每个元素都能通过给定的函数的测试,则返回true,反之falseif (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 = !!www.gsm-guard.net(context, this[k], k, this);}}return passed;};}// 类似every函数,但只要有一个通过给定函数的测试就返回trueif (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 = !!www.gsm-guard.net(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; 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  -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 = 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对象扩展代码如下所示:
飘零金盾1.3源码,vscode是啥,在其他电脑运行ubuntu,怎么入侵tomcat,sqlite 能卸载吗,html音乐代码插件下载,前端vue框架的搭建,爬虫知识100条,三级联动 php,seo外包 上海翼好,网站登入页面源码,网页图标下载,清晰社区模板,页面搜索 代码,c 车辆管理系统源代码,vivi小偷程序如何开始采集lzw
/**
* 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
asp源码收款系统,ubuntu如何关闭休眠,爬虫统计爬取,南昌php吧,seo做bclzw
Posted in 未分类 | Tagged js_array扩展, js_string扩展