1

I have an object like below

 function obj() {

     this.cellSize = 50;

     this.createBlock = function() { // creates a block object
        this.x = 0;
        this.y = 0 - (this.cellSize * 1.5);
        this.baseVelocity = 1;
        this.velocity = this.baseVelocity;
        return this;
    };

    this.activeBlock = this.createBlock(); // active block object

    this.nextBlock = this.createBlock(); // next block object
 }

When I check obj.activeBlock I am not getting the object which should be returned from obj.createBlock?

Thanks,

5
  • 1
    Are you using new keyword? var a = new obj(); console.log(a.activeBlock);. Commented Feb 25, 2013 at 6:06
  • Yes I am but obj.activeBlock is the same as obj, rather than the object having what is outlined in createBlock? Commented Feb 25, 2013 at 6:10
  • "When I check obj.activeBlock..." Seems you're guessing at how this in JavaScript works. Commented Feb 25, 2013 at 6:11
  • @thesystem I am not guessing just trying to understand, I have only been learning for 2 days Commented Feb 25, 2013 at 6:14
  • @ShaShads: Alright, well there's no assignment of .activeBlock to obj. The obj is a function, and this isn't a reference to it. Commented Feb 25, 2013 at 6:16

1 Answer 1

2

You probably want something like:

function obj() {
     var that = this;
     this.cellSize = 50;

     this.createBlock = function() { // creates a block object
        this.x = 0;
        this.y = 0 - (that.cellSize * 1.5);
        this.baseVelocity = 1;
        this.velocity = this.baseVelocity;
        return this;
    };

    this.activeBlock = new this.createBlock(); // active block object

    this.nextBlock = new this.createBlock(); // next block object
}

The this in the createBlock function should be different from the this of obj(). You also need to create a new object using new for each block. If cellSize is supposed to be a constant, you can rewrite the code as a closure:

function obj() {
     var cellSize = 50;

     this.createBlock = function() { // creates a block object
        this.x = 0;
        this.y = 0 - (cellSize * 1.5);
        this.baseVelocity = 1;
        this.velocity = this.baseVelocity;
        return this;
    };

    this.activeBlock = new this.createBlock(); // active block object

    this.nextBlock = new this.createBlock(); // next block object
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you this was exactly where I was going wrong, thanks for the closure tip :)
@TedHopp: If OP did that, the function would no longer have access to the variable scope being used in your answer. And since .createBlock is invoked as a constructor, it also wouldn't have access to any properties of the object on which it was invoked.
@thesystem - Yeah, I just realized that. Best would be a factory function rather than a constructor. Deleting the comment.

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.