Williammer
6/16/2014 - 7:51 AM

jsMethod.borrowArray.js - borrow native methods of Array, ensure compatibility by not subclass Array.

jsMethod.borrowArray.js - borrow native methods of Array, ensure compatibility by not subclass Array.

function MyArray() {}
//MyArray.prototype = new Array(); //subclass Array, which is not compatible across all browsers.

MyArray.prototype.length = 0;
(function () {
    var methods = ['push', 'pop', 'shift', 'unshift',
        'slice', 'splice', 'join'];
    for (var i = 0; i < methods.length; i++)(function (name) {
        MyArray.prototype[name] = function () {
            return Array.prototype[name].apply(this, arguments);
        };
    })(methods[i]);
})();
var mine = new MyArray();
mine.push(1, 2, 3);
assert(mine.length == 3,
    "All the items are on our sub-classed array.");
assert(!(mine instanceof Array),
    "We aren't subclassing Array, though.");