List.js 737 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. define(function () {
  2. /**
  3. * Simple implementation of the List abstract data type from ECMA 402.
  4. *
  5. * @constructor
  6. * @private
  7. */
  8. var List = function () {
  9. this.length = 0;
  10. };
  11. List.prototype.forEach = function (f) {
  12. for (var i = 0; i < this.length; i++) {
  13. f(this[i]);
  14. }
  15. };
  16. List.prototype.push = function (item) {
  17. this[this.length] = item;
  18. this.length++;
  19. };
  20. List.prototype.indexOf = function (item) {
  21. for (var i = 0; i < this.length; i++) {
  22. if (this[i] === item) {
  23. return i;
  24. }
  25. }
  26. return -1;
  27. };
  28. List.prototype.toArray = function () {
  29. var i = 0;
  30. var result = new Array(this.length);
  31. while (i < this.length) {
  32. result[i] = this[i];
  33. i++;
  34. }
  35. return result;
  36. };
  37. return List;
  38. });