Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> OOP i dziedziczenie, Iterator -> InfiniteIterator
mrok
post 29.04.2011, 21:51:31
Post #1





Grupa: Zarejestrowani
Postów: 258
Pomógł: 17
Dołączył: 22.05.2007

Ostrzeżenie: (0%)
-----


Mam sobie taki oto kodzik:


  1. var xsApp = window.xsApp || {};
  2.  
  3. /**
  4. * @namespace xsApp
  5. * @class util
  6. */
  7. xsApp.util = {};
  8.  
  9. /**
  10. * @namespace xsApp.util
  11. * @class iterator
  12. */
  13. xsApp.util.iterator = {};
  14.  
  15. /**
  16. * Ensure prototypal inheritance
  17. * @namespace xsApp
  18. * @class util
  19. * @method createObject
  20. * @param baseObj Object which properties and method should be inherited
  21. * @return Object
  22. */
  23. xsApp.util.createObject = function (baseObj) {
  24. function F(){}
  25. F.prototype = baseObj;
  26. return new F();
  27. };
  28.  
  29. /**
  30. * @namespace xsApp.util
  31. * @class iterator
  32. * @method getIterator
  33. * @param arr Array used to build iterator
  34. * @return Iterator
  35. */
  36. xsApp.util.iterator.getIterator = function (arr) {
  37. var list = arr,
  38. index = 0,
  39. length = list.length;
  40. if ($.isArray(list)){
  41. return {
  42. first : function () {
  43. return list[0];
  44. },
  45. next : function () {
  46. return list[index++] || null;
  47. },
  48. prev : function () {
  49. return list[--index] || null;
  50. },
  51. current : function () {
  52. return list[index] || null;
  53. },
  54. reset : function () {
  55. return this.seek(0);
  56. },
  57. seek : function (newIndex) {
  58. index = newIndex;
  59. return this;
  60. },
  61. hasNext : function () {
  62. return index < length;
  63. },
  64. hasPrev : function () {
  65. return index > 0;
  66. },
  67. getList : function () {
  68. return list;
  69. },
  70. getCurrentIndex : function () {
  71. return index;
  72. },
  73. getListLength : function () {
  74. return length;
  75. }
  76. };
  77. }
  78. return Error ('xsApp.util.iterator.getIterator - parameter have to be an array');
  79. };
  80.  


potrzebuję też dopisać infiniteIterator, tylko zamiast kopiowac mnustwo kodu chciałbym jakos go wydziedziczyc z poprzedniego iteratora.
Cos jak

  1. /**
  2. * @namespace xsApp.util
  3. * @class iterator
  4. * @method getInfiniteIterator
  5. * @param arr Array used to build iterator
  6. * @return InfiniteIterator
  7. */
  8. xsApp.util.iterator.getInfiniteIterator = function (arr) {
  9. var iter = xsApp.util.iterator.getIterator(arr);
  10. iter = xsApp.util.createObject(iter);
  11.  
  12. iter.hasNext = function () {
  13. return true;
  14. };
  15. iter.hasPrev = function () {
  16. return true;
  17. };
  18. //next i prev oczywiscie podobnie
  19. return inter;
  20. };
  21.  



Ma ktoś pomysł jak to zrobić?
Oczywiście chciałbym uniemożliwość dostęp do zmiennych w iteratorze (cos jak private i byc moze z tym bedzie problem).





--------------------
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 06:06