简体   繁体   English

Array.Splice原型

[英]Array.Splice prototype

I have a addAfter and addBefore function that adds a new element to an array. 我有一个addAfter和addBefore函数,它将新元素添加到数组中。 This array is my storage that other functions use. 这个数组是我的其他功能使用的存储。 Basically I am storing low level objects that define table cells. 基本上,我存储定义表单元格的低级对象。 After the element is added to the array I then have to insert the value of the html property of the element the table row. 将元素添加到数组后,我必须在表行中插入元素的html属性值。

Is there a way to prototype my array to handle both operations rather than me having to double up on the work load every time I addAfter or addBefore, with-out messing up the prototype of the native array? 有没有一种方法可以对我的数组进行原型处理以处理这两个操作,而不必每次都使addAfter或addBefore倍增工作量,而又不会弄乱本机数组的原型?

var bays=[];

 addAfter: function (b, n) {
            for (var i = 0, ii, len = bays.length; i < len; i++) {
                ii = i + 1; if (ii == n) {
                    bays.splice(ii, 0, b);
                    var newCell = canvsTrBay.insertCell(ii);
                    newCell.outerHTML = b._html;
                };
            };
            this.build();
        }

Is it possible to do something like: 是否可以做类似的事情:

   bays.prototype.add=function(b,n,isAfter){

       for (var i = 0, ii, len = bays.length; i < len; i++) {
                    ii =(isAfter? (i + 1):(n>0?i-1:0); 
                     if (ii == n) {
                        bays.splice(ii, 0, b);
                        var newCell = canvsTrBay.insertCell(ii);
                        newCell.outerHTML = b._html;
                    };
                };
                this.build();
    }

您可以将其直接添加到对象本身:

bays.add = ...;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM