Mam sobie taki oto kodzik:
var xsApp = window.xsApp || {};
/**
* @namespace xsApp
* @class util
*/
xsApp.util = {};
/**
* @namespace xsApp.util
* @class iterator
*/
xsApp.util.iterator = {};
/**
* Ensure prototypal inheritance
* @namespace xsApp
* @class util
* @method createObject
* @param baseObj Object which properties and method should be inherited
* @return Object
*/
xsApp.util.createObject = function (baseObj) {
function F(){}
F.prototype = baseObj;
return new F();
};
/**
* @namespace xsApp.util
* @class iterator
* @method getIterator
* @param arr Array used to build iterator
* @return Iterator
*/
xsApp.util.iterator.getIterator = function (arr) {
var list = arr,
index = 0,
length = list.length;
if ($.isArray(list)){
return {
first : function () {
return list[0];
},
next : function () {
return list[index++] || null;
},
prev : function () {
return list[--index] || null;
},
current : function () {
return list[index] || null;
},
reset : function () {
return this.seek(0);
},
seek : function (newIndex) {
index = newIndex;
return this;
},
hasNext : function () {
return index < length;
},
hasPrev : function () {
return index > 0;
},
getList : function () {
return list;
},
getCurrentIndex : function () {
return index;
},
getListLength : function () {
return length;
}
};
}
return Error ('xsApp.util.iterator.getIterator - parameter have to be an array');
};
potrzebuję też dopisać infiniteIterator, tylko zamiast kopiowac mnustwo kodu chciałbym jakos go wydziedziczyc z poprzedniego iteratora.
Cos jak
/**
* @namespace xsApp.util
* @class iterator
* @method getInfiniteIterator
* @param arr Array used to build iterator
* @return InfiniteIterator
*/
xsApp.util.iterator.getInfiniteIterator = function (arr) {
var iter = xsApp.util.iterator.getIterator(arr);
iter = xsApp.util.createObject(iter);
iter.hasNext = function () {
return true;
};
iter.hasPrev = function () {
return true;
};
//next i prev oczywiscie podobnie
return inter;
};
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).