Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Assigning class properties in Javascript


    Class properties can help you simplify your classes code. In this lesson we will see how to use class properties in javascript classes.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Yonatan Kra: I'll create a class Recipe. It will get a name and store it in a property. Now I'll create a new class Pasta that extends Recipe. Inside I'll add a constructor that defines name and tasty properties.

    I inaudible in the browser and go to the console and instantiate a new Pasta. This results in an error because an extension class must call the super method in its constructor.

    Going back to the code I'll replace the name assignment with the super method and pass the name. Back in the console, I'll refresh and instantiate a new Pasta. The Pasta was instantiated correctly.

    Looking at our code, it starts to look cumbersome. If I add more properties to Pasta, the constructor will get loaded. We can solve this by defining class properties. Above the constructor I'll define the properties tasty and type. We can now delete the whole constructor.

    Heading back to the console, we instantiate a new Pasta and see that is working, with the simplified code.