Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript static variable in class

class Thing {
  static type = 'thing';
  static myType() {
    return `This class has a type of ${this.type}`;
  }
}
console.log(Thing.type);
//=> 'thing'
console.log(Thing.myType());
//=> 'This class has a type of thing'

// Instances do not inherit static fields and methods
const t = new Thing();
console.log(t.type);
//=> undefined
console.log(t.myType())
//=> Uncaught TypeError: t.myType is not a function 
 
PREVIOUS NEXT
Tagged: #javascript #static #variable #class
ADD COMMENT
Topic
Name
7+8 =