Javascript Class Static Members

Introduction

ECMAScript 6 classes create static members by adding static before the method or accessor property name.

class PersonClass {//from w w  w .  j  a  va2s  . c o m

    // equivalent of the PersonType constructor
    constructor(name) {
        this.name = name;
    }

    // equivalent of PersonType.prototype.sayName
    sayName() {
        console.log(this.name);
    }

    // equivalent of PersonType.create
    static create(name) {
        return new PersonClass(name);
    }
}

let person = PersonClass.create("CSS");

Static members are not accessible from instances.

You must always access static members from the class directly.




PreviousNext

Related