Static and Instance Variables in Classes (Preview)

Though the ES6 does not support static and instance variables, there is a proposal for ES7 that would enable this functionality.

The ES7 property proposal would allow for both static and instance variables to be declared on a class:

class Animal {
     * Declare "legs" as a static property of animal with a default value of 4
    static legs = 4;

     * Declare the "name" property of animal without a value

All static and instance variables must be declared directly inside of the class. Static variables are simply declared by prefixing the variable name with static. If you wish to include an initial value, you may specify the value by assigning it as you would a normal variable. If you do not want to include an initial value, you may just specify the variable name followed by a semicolon to end the line.

Lesson tags: ES7, Programming
Back to: Modern JavaScript > Classes and Functions