Advertisement
Guest User

Untitled

a guest
Apr 16th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. module.exports = Collection;
  2.  
  3. /**
  4. * Конструктор коллекции
  5. * @constructor
  6. */
  7.  
  8. var arr;
  9. function Collection() {
  10. this.arr = [];
  11. this.getStorage().forEach(e => arr.push(e));
  12. }
  13.  
  14.  
  15. Collection.prototype.getStorage = function(){
  16. var a = [];
  17. return a;
  18. };
  19. // Методы коллекции
  20. Collection.prototype.values = function () {
  21. return this.arr;
  22. };
  23. Collection.prototype.at = function (idx) {
  24. return idx == 0 ? null : this.values()[idx-1];
  25. };
  26.  
  27. // другие методы
  28. Collection.prototype.append = function (value) {
  29. if (value instanceof Collection ) {
  30. console.log('на вход получена коллекция');
  31. console.log(value);
  32. var array = value.values();
  33. console.log('коллекция содержит массив');
  34. console.log(array);
  35. var col = value;
  36. col.values().forEach(e => this.values().push(e));
  37. }else{
  38. Array.isArray(value) && (value.forEach(e => this.arr.push(e)));
  39. this.arr.push(value);
  40.  
  41. }
  42. };
  43.  
  44.  
  45. Collection.prototype.removeAt = function(idx){
  46. idx = idx-1;
  47. if (idx >= 0 && idx <= this.values().length){
  48. this.values().splice(idx,1);
  49. return true;
  50. } else{
  51. return false;
  52. }
  53.  
  54.  
  55. }
  56.  
  57. Collection.prototype.count = function () {
  58. return this.arr.length;
  59. };
  60.  
  61. /**
  62. * Создание коллекции из массива значений
  63. */
  64. Collection.from = function (arrr) {
  65. var c = new Collection();
  66. arrr.forEach(e => c.append(e));
  67. return c;
  68.  
  69. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement