1

I'm stuck in some part of my code. I created something like model class BoardEx and I want to create Iterator for it, here's some example:

Game.BoardEx = function(params){
    ...
    this.board = Array(cols, rows);
    ...
}

where Game is the "parent" object of all children "classes". Then I declared Iterator:

Game.BoardEx.prototype.Iterator = function(){
    var pos = {x: 0, y: -1};
    this.next = function(){
        ...     
        return this.board[pos.x][pos.y];
    };

Later usage is:

var board = new Game.BoardEx();
var iter = new board.Iterator();

It seems that I can't call this.board in this scope. Should I use bind or other method or just can't I call this at all?

4
  • Aren't you missing a new Commented Jun 5, 2014 at 14:00
  • This looks like it should work, assuming you're doing new Iterator and copying the board over (or a reference to it). Remember, this.board belongs to the created Iterator now and not the BoardEx object. Also, you want a method like .iterate on the BoardEx instance that does return new Iterator(this) with the board passed so you can have access to it. Commented Jun 5, 2014 at 14:01
  • I use it later as: var board = new Game.BoardEx(); var iter = new board.Iterator(); Commented Jun 5, 2014 at 14:02
  • @user3664767 Ah, I understand your problem much better now, please edit that into the question Commented Jun 5, 2014 at 14:03

1 Answer 1

2

You can't access this of the calling object when you're inside a new call.

Performing a new "construct" call changes the context to the newly created object.

Instead, you can use a builder function:

Game.BoardEx.prototype.getIterator = function(){
    function Iterator(board){
        this.board = board;
        this.pos = {x:0, y:-1};
    }
    Iterator.prototype.next = function(){
       return this.board[this.pos.x][this.pos.y];
       ...
    }
    return new Iterator(this.board); // create new instance with the board.
}

You can define Iterator completely externally to the Board, as long as the getIterator function passes a valid board to it. This would also let you call it from the outside and might be more effecient. It will also make instanceof work but IMO it's really not a big deal.

Sign up to request clarification or add additional context in comments.

3 Comments

I was just writing the same answer. +1 for GetIterator on the BoardEx instance. Definitely worth hiding the Interator constructor in closure, but maybe in the scope just above this function.
Okay, it worked like magic, but can I modify this.board in this scope or just iterate through it? E.g var it = new board.getIterator(); it.setval(123);
@user3664767 since board is an object, it is passed by reference value, you can change the contents of the board from the iterator, (but not swap the board altogether).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.