I have a little problem which I can't solve on my own. The problem is as follows: I've implemented a queue object, which for the simplicity I call q. This object has 2 methods: put(x) and get(), respectively to insert a value into and retrieving from the queue. There's also a third method 'With(constructor)'. This constructor can be Doubling, Incrementing and Filtering. The purpose of the With method is that it should add the constuctor in the inheritance chain of o. Afer the call of the With method the input for q should be doubled, incremented or filtered, depending on the constructor.

This is my implementation so far:

// function BasicIntQueue(){ this.l = []; } function Filtering(){ } Filtering.prototype.put = function(x){ if (x >= 0){ this.l.unshift(x); } }; function Incrementing(){ } Incrementing.prototype.put = function(x) { this.l.unshift(x + 1); }; function Doubling(){ } Doubling.prototype.put = function(x) { this.l.unshift(2 * x); }; BasicIntQueue.prototype.put = function(x) { this.l.unshift(x); }; BasicIntQueue.prototype.get = function() { return this.l.pop(); }; BasicIntQueue.prototype.With = function(cons){ var q = Object.create(this); for(var p in cons.prototype){ o[p] = cons.prototype[p]; } return q; };

This works fine for the following:

var q2 = new BasicIntQueue(); q2.put(6); q2 = q2.With(Doubling); q2.put(7); q2.get()// This gives 6 as result q2.get()//This gives 14 as result

But when I run the following code, I don't get the correct result:

var q3 = new BasicIntQueue(); q3.put(13); q3 = q3.With(Incrementing).With(Doubling); q3.put(21); q3.get() //gives 13 q3.get() //gives 42, where 43 is expected

From here follows that only the last constructor is being executed, where all constructors should be. I have double checked my code and there are no typo's or anything like that.

My question is how can I solve this problem?

More concretely, what am I overseeing to get the correct output? Any suggestions, improvements or how I can do things differently are more then welcome.