Search code examples
javascriptobjectsequencedelegation

Sequence from Class OO to Object Delegation pattern


My intention is to use function logFive2 to iterate over a sequence object like ArraySeq2 or RangeSeq2 although I want to create the RangeSeq2 using Object delegation pattern and stay away from Class like way(ArraySeq2). What I am doing wrong with RangeSeq2? My code doesn't work because logFive2 does not iterate over RangeSeq2 and I cannot see why. If you have any idea about what goes wrong please let me see. Thank you.

   function logFive2(sequence){
    for(var i = 0; i < 5 && sequence != null; i++){
      console.log(sequence.head());
      sequence = sequence.rest();
    }
    }

    function ArraySeq2(array,offset){
      this.array = array;
      this.offset = offset;
    }

    ArraySeq2.prototype.rest  = function(){
         console.log("to follow " + this.offset);
      return ArraySeq2.make(this.array,this.offset + 1);
    };

    ArraySeq2.prototype.head = function(){
      return this.array[this.offset];
    };

    ArraySeq2.make = function(array,offset){
      if(offset == null) offset = 0;
      if(offset >= array.length)
        return null;
      else return new ArraySeq2(array,offset);
    }
    logFive2(ArraySeq2.make([1, 2,5,6,9,11]));
    // → 1
    // → 2

The part above works fine ______________ RangeSeq2 object it is my problem

    var RangeSeq2 = {
      init: function(from,to){
        this.from = from;
        this.to = to;
      },
      rest: function(){
        if (from > to) 
          return null;
        else
          return this.init(this.from + 1,this.to);
      },
      head: function(){
        return this.from;
      }
    };

    var RangeTT = Object.create(RangeSeq2);
    RangeTT.init(100,1000);
    logFive2(RangeTT.init(100,1000));

Solution

  •  function logFive2(sequence){
        for(var i = 0; i < 5 ; i++){
          console.log(sequence.head());
          sequence.rest();
        }
        }
    
         var RangeSeq2 = {
          rest: function(){
            if (this.from > this.to) {
              return null;
            }
            else
              return this.from += 1,this.to;
          },
          head: function(){
            return this.from;
          }
        };
    
        var RangeTT = Object.create(RangeSeq2);
       RangeTT.from = 100;
       RangeTT.to = 1000;
       logFive2(RangeTT);
       //100
       //101
       //102
       //103
       //104
    

    Sorted out! the problem was so much simpler than I thought will be. My problem was trying to do an unhealthy mixture of classical inheritance and instantiation over the Object delegation because I didn't understood how it works. Soon as I managed to understand how "this" works and soon I understood Object.create (which is very powerful ) , the __proto__ and knew the difference it has compared to function Object.prototype I could find a solution.

    1.My first mistake I think was trying to create state in the object by calling the method init() without having a property to hold the values in the object.

    2.The rest() method would query on variables which would not exist on the object.

    I have to mention that I had to change the iterator function LogFive2() to be suitable for the object delegation design in my case.