Vinyl Windows
Vinyl Windows

Reputation: 503

Array.Splice prototype

I have a addAfter and addBefore function that adds a new element to an array. 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.

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?

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();
    }

Upvotes: 0

Views: 141

Answers (1)

SLaks
SLaks

Reputation: 887285

You can add it directly to the object itself:

bays.add = ...;

Upvotes: 1

Related Questions