Example 1 — Static fields
Shared class data.
JavaScript
class A { static version = 1; }
console.log(A.version); How It Works
- Stored on the constructor.
static members live on the constructor function. Call them as Class.method(), not on instances. Subclasses inherit static members.
class MathUtil {
static double(n) { return n * 2; }
}
console.log(MathUtil.double(4));
console.log(new MathUtil().double); Shared class data.
class A { static version = 1; }
console.log(A.version); Child can call parent statics.
class A { static id(){ return "A"; } }
class B extends A {}
console.log(B.id()); this is the class.
class A { static who(){ return this.name; } }
console.log(A.who()); CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful