CodeIgniter Forums

Full Version: How does this work: super()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello gentlemen!
I have been working with classes, and now I run into class prototyping! I understand what it does, (super() refers to the parent class and allows the child class to access the parent’s methods and properties) but I do not understand how it works or what it means. Why would you need access to the parent’s properties name and width for the child class? Spudnik is not calling on the parent class, but the PotatoPlanet class. I don’t understand it. The class below extends another class. (You can check chapter 20 for the parent class).

Code:
    class PotatoPlanet extends Planet {
        constructor(name, width, potatoType) {
            super(name, width);
            this.potatoType = potatoType;
        }
        getPotatoType() {
            let thePotato = this.potatoType.toUpperCase() + "!!l!!!";
            console.log(thePotato);
            return thePotato;
        }
    }
    let spudnik = new PotatoPlanet("Spudnik", 12411, "Russet");
    spudnik.gravity = 42.1;
    spudnik.getPotatoType();
    console.log(spudnik);