Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
How to implement abstract factory in js?

I want to implement very simple abstract factory pattern but I am facing this error. Also am I doing something wrong with my code? How can I improve this to make it work properly?

Must call super constructor in derived class before accessing 'this' or returning from derived constructor"

class Pizza {
    constructor(name,size,type) {
        this.name = name;
        this.size = size;
        this.type = type;
    }

    prepare() {
        console.log("prepare");
    }
    bake() {
        console.log("bake");
    }
    deliver() {
        console.log("deliver");
    }
}

class MeatPizza extends Pizza {
    constructor() {
        super("Chicago style pizza", "Large", "Meat")
    }
}

class VegePizza extends Pizza {
    constructor() {
        super("Only natural", "Large", "Vegetarian")
    }
}

class MakePizza {
    createPizza(type) {
        switch (type) {
            case "meat":
                return new MeatPizza()
            case "vege":
                return new VegePizza()
            default:
                throw new Error("Something went wrong...");
        }
    }
}

class MakeMeatPizza extends MakePizza {
    constructor() {
        super.createPizza("meat");
    }
}

class MakeVegePizza extends MakePizza {
    constructor() {
        super.createPizza("vege");
    }
}

class OrderPizza {

}

const test = new MakeVegePizza();
console.log(test)

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!