js static in class code example

Example 1: get static methods of class javascript

class Hi {
	constructor() {
      console.log("hi");
    }
   	my_method(){}
  	static my_static_method() {}
}

function getStaticMethods(cl) {
  return Object.getOwnPropertyNames(cl)
}

console.log(getStaticMethods(Hi))
// => [ 'length', 'prototype', 'my_static_method', 'name' ]

Example 2: javascript class static fields

class ClassWithInstanceField {
  instanceField = 'instance field'
}

class ClassWithStaticField {
  static staticField = 'static field'
}

Tags:

Misc Example