2

I would like to create array or object of objects while calling constructor of class? I mean, everytime I'm calling class constructor it adds new object to array or object.

f.e.

let blocks = {};

class Block(){

    constructor(X,Y,width,height){
        this.X = X;
        this.Y = Y;
        ...
        and now I would like to add this created object to "blocks"
        something like: blocks.push(and this object here)
    }
}

How can I implement something like this? Or should it be another function?

1
  • You can add another method inside class and call this metod in constructor. Commented Aug 19, 2018 at 10:57

2 Answers 2

3

something like: blocks.push(and this object here)

That's exactly how you'd do it, but blocks should be an array, not an object. So:

let blocks = [];

and then in the constructor:

blocks.push(this);

Live Example:

let blocks = []; // <== [], not {}

class Block {    // No ()

    constructor(X,Y,width,height){
        this.X = X;
        this.Y = Y;
        blocks.push(this);
    }
}

new Block(1, 1, 1, 1);
new Block(2, 2, 2, 2);
new Block(3, 3, 3, 3);

console.log(blocks.length); // 3

But, beware that that means the object (the block that was created) will always be retained in memory, even if whatever created it originally finishes with it and lets it go. Normally, you wouldn't have a constructor do something like this, you'd leave it to the caller.

Depending on why you're building this list of blocks, you may (or may not) want to use WeakSet or WeakMap instead.

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

8 Comments

And concerning the WeakSet/Maps: they are not really useful as one cannot iterate them
OMG.. Literally I spend at least half an hour to try to implement it. And I didn't think about simple "this". Thanks a lot ;)
@JonasWilms - Well again, that depends on what the use case is. Traditional use of WeakMap is private per-instance information, so you don't need iteration. I've never actually needed a WeakSet, but I've heard there are use cases for it. ;-)
@WojciechJakubek - Not sure how getters/setters would relate to using a WeakMap (for or against). :-)
@WojciechJakubek - If your goal is to have a list of blocks to draw on a canvas, I recommend not having the Block constructor add the block to the list. Have the code calling the constructor do it. Make that code responsible for adding to (and possibly removing from) the list.
|
0

Your cannot the push method because it is not present on an object. Currently your created an object by:

let blocks = [];  // creates object with literal syntax.

push is a method which is only available on arrays:

let obj = {};
let arr = [];

console.log(typeof obj.push );  // push not on obj
console.log(typeof arr.push );  // push is on array.prototype

You can solve it by changing from an object literal to an array literal.

let blocks = []; 

class Block {    

    constructor(X,Y,width,height){
        this.X = X;
        this.Y = Y;
        blocks.push(this);
    }
}

new Block(1, 2, 3, 4);
new Block(2, 4, 6, 8);

console.log(blocks); // 3

Comments

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.