zoukankan      html  css  js  c++  java
  • 扩展原生js的Array类

    /**************** 数组扩展 ********************/
    
    Array.prototype.add = function(item) {
        this.push(item);
    }
    
    Array.prototype.addRange = function(items) {
        var length = items.length;
    
        if (length != 0) {
            for (var index = 0; index < length; index++) {
                this.push(items[index]);
            }
        }
    }
    
    Array.prototype.clear = function() {
        if (this.length > 0) {
            this.splice(0, this.length);
        }
    }
    
    Array.prototype.isEmpty = function() {
        if (this.length == 0)
            return true;
        else
            return false;
    }
    
    Array.prototype.clone = function() {
        var clonedArray = [];
        var length = this.length;
    
        for (var index = 0; index < length; index++) {
            clonedArray[index] = this[index];
        }
    
        return clonedArray;
    }
    
    Array.prototype.contains = function(item) {
        var index = this.indexOf(item);
        return (index >= 0);
    }
    
    Array.prototype.dequeue = function() {
        return this.shift();
    }
    
    Array.prototype.indexOf = function(item) {
        var length = this.length;
    
        if (length != 0) {
            for (var index = 0; index < length; index++) {
                if (this[index] == item) {
                    return index;
                }
            }
        }
    
        return -1;
    }
    
    Array.prototype.insert = function(index, item) {
        this.splice(index, 0, item);
    }
    
    Array.prototype.joinstr = function(str) {
        var new_arr = new Array(this.length);
        for (var i = 0; i < this.length; i++) {
            new_arr[i] = this[i] + str
        }
        return new_arr;
    }
    
    Array.prototype.queue = function(item) {
        this.push(item);
    }
    
    Array.prototype.remove = function(item) {
        var index = this.indexOf(item);
    
        if (index >= 0) {
            this.splice(index, 1);
        }
    }
    
    Array.prototype.removeAt = function(index) {
        this.splice(index, 1);
    }
  • 相关阅读:
    详解用em替换px
    js判断是否为ie浏览器
    nth-child()选择器小结
    HTML5之canvas
    MQTT-SN协议乱翻之消息格式
    MQTT-SN协议乱翻之简要介绍
    MQTT 3.1.1,值得升级的6个新特性
    MQTT 3.1协议非严肃反思录
    MQTT协议笔记之mqtt.io项目HTTP协议支持
    MQTT协议笔记之mqtt.io项目Websocket协议支持
  • 原文地址:https://www.cnblogs.com/zfc2201/p/2820342.html
Copyright © 2011-2022 走看看