Hmm, I don't think interfaces are particularly relevant for JavaScript. It's all duck-typed and dynamically dispatched. There are no safety/speed issues that would be affected.
Inheritance can still work. You can add or replace methods on the returned object, allowing it to be extended. If one factory function calls another, you can wrap an additional closure around new methods, providing for more private data. (Without new/replaced methods, there would be no reason to capture additional variables). One limitation, is not being able to directly access variables in the old closure.
/* Counter with reset */
function generateCounter() {
var count = 0;
function reset() {
count = 0;
}
function next() {
return count++;
}
/* Return an object with two public member functions, which both have implicit access to the same closure for private data */
return {
reset: reset,
next: next
};
}
function generateEvenCounter() {
var obj = generateCounter();
var oldNext = obj.next;
obj.next = function newNext() {
/*return count += 2;*/ /* No access to old closure, 'count' is not in scope here */
var retVal = oldNext();
oldNext();
return retVal;
}
return obj;
}
var counter = generateEvenCounter();
console.log(counter.next(), counter.next(), counter.next()); /* 0 2 4 */
console.log();
counter.reset();
console.log(counter.next(), counter.next()); /* 0 2 */
Though again, there are Constructor Functions, and the new Class syntax. Probably a better way to express your intent, and I suspect will result in more efficient runtime performance.