s9s-objects
Version:
Global object container, used across projects
95 lines (82 loc) • 1.79 kB
JavaScript
;
exports = module.exports = AbstractArray;
/**
* @constructor
* @abstract
* @extends {Array}
*/
function AbstractArray() {
for (var i = 0; i < arguments.length; i++) {
this.push(arguments[i]);
}
}
/**
* @type {Array}
*/
AbstractArray.prototype = [];
/**
* @type {AbstractArray}
*/
AbstractArray.prototype.constructor = AbstractArray;
/**
* @param method
* @param args
* @returns {AbstractArray}
*/
AbstractArray.prototype.fakeMethod = function (method, args) {
var instance = new this.constructor();
this.constructor.apply(instance, Array.prototype[method].apply(this, args));
return instance;
};
/**
* Override slice method to return instance of current object
* @returns {AbstractArray}
*/
AbstractArray.prototype.slice = function () {
return this.fakeMethod('slice', arguments);
};
/**
* Override method to return object
* @returns {AbstractArray}
*/
AbstractArray.prototype.splice = function () {
return this.fakeMethod('splice', arguments);
};
/**
* Copy this object
* @returns {AbstractArray}
*/
AbstractArray.prototype.copy = function () {
var copy = new this.constructor();
this.constructor.apply(copy, this.slice());
return copy;
};
/**
* Returns array size
* @returns {Number}
*/
AbstractArray.prototype.size = function () {
return this.length;
};
/**
* Returns first element
* @returns {*}
*/
AbstractArray.prototype.first = function () {
return this.size() > 0 ? this[0] : null;
};
/**
* Returns last element
* @returns {*}
*/
AbstractArray.prototype.last = function () {
return this.size() > 0 ? this(this.size() - 1) : null;
};
/**
* @param item
* @returns {*}
*/
AbstractArray.prototype.prepend = function (item) {
this.unshift(item);
return this;
};