Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Global variable in class

I have a below class Square,

Now the count variable is in Global space irrespective of Square class,

Can I declare count variable inside class to make it a class global variable ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

count=0;
class Square{
    constructor(sides){
    this.sides = sides;
    count++;
    }
    getArea(){
    count++;
    return this.sides*this.sides;
    }
    setSides(sides){
    count++;           
    this.sides = sides;
    }
    static getCurrentSides(){
    return count;
    }
}
const square = new Square(20);
console.log(square.getArea());
square.setSides(8);
console.log(square.getArea());
console.log(Square.getCurrentSides());

>Solution :

If I understand you correctly, you want the count variable to be non-global. This could mean two things. Either you want it to be accessible from outside but not on the global object (window in browser contexts) or you want it to be inaccessible from outside the class.

If it’s the first, you can declare it inside the class but make it static:

class Square {
    static count = 0
    constructor(sides){
        this.sides = sides;
        Square.count++;
    }
    getArea(){
        Square.count++;
        return this.sides*this.sides;
    }
    setSides(sides){
        Square.count++;           
        this.sides = sides;
    }
    static getCurrentSides(){
        return Square.count;
    }
}

const square = new Square(20);
console.log(square.getArea());
square.setSides(8);
console.log(square.getArea());
console.log(Square.getCurrentSides());

This way, Square.count will still be accessible (readable and writable) wherever Square is accessible.

If you want count to be entirely private, you can create a block-scoped variable:

let Square;
{
    let count = 0;
    Square = class Square {
        constructor(sides){
            this.sides = sides;
            count++;
        }
        getArea(){
            count++;
            return this.sides*this.sides;
        }
        setSides(sides){
            count++;           
            this.sides = sides;
        }
        static getCurrentSides(){
            return count;
        }
    }
}

const square = new Square(20);
console.log(square.getArea());
square.setSides(8);
console.log(square.getArea());
console.log(Square.getCurrentSides());

The same could also be accomplished using a private static field, but that’s only recently become available so support may vary:

class Square {
    static #count = 0
    constructor(sides){
        this.sides = sides;
        Square.#count++;
    }
    getArea(){
        Square.#count++;
        return this.sides*this.sides;
    }
    setSides(sides){
        Square.#count++;           
        this.sides = sides;
    }
    static getCurrentSides(){
        return Square.#count;
    }
}

const square = new Square(20);
console.log(square.getArea());
square.setSides(8);
console.log(square.getArea());
console.log(Square.getCurrentSides());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading