Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports = Collection;
- /**
- * Конструктор коллекции
- * @constructor
- */
- var arr;
- function Collection() {
- this.arr = [];
- this.getStorage().forEach(e => arr.push(e));
- }
- Collection.prototype.getStorage = function(){
- var a = [];
- return a;
- };
- // Методы коллекции
- Collection.prototype.values = function () {
- return this.arr;
- };
- Collection.prototype.at = function (idx) {
- return idx == 0 ? null : this.values()[idx-1];
- };
- // другие методы
- Collection.prototype.append = function (value) {
- if (value instanceof Collection ) {
- console.log('на вход получена коллекция');
- console.log(value);
- var array = value.values();
- console.log('коллекция содержит массив');
- console.log(array);
- var col = value;
- col.values().forEach(e => this.values().push(e));
- }else{
- Array.isArray(value) && (value.forEach(e => this.arr.push(e)));
- this.arr.push(value);
- }
- };
- Collection.prototype.removeAt = function(idx){
- idx = idx-1;
- if (idx >= 0 && idx <= this.values().length){
- this.values().splice(idx,1);
- return true;
- } else{
- return false;
- }
- }
- Collection.prototype.count = function () {
- return this.arr.length;
- };
- /**
- * Создание коллекции из массива значений
- */
- Collection.from = function (arrr) {
- var c = new Collection();
- arrr.forEach(e => c.append(e));
- return c;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement