jskatas.org Continuously Learn JavaScript. Your Way.

Class: static

Use of the static keyword inside a class.

Inside a class you can use the static keyword

for methods

a static method just has the prefix static
class UnitTest {} class TestFactory { makeTest() { return new UnitTest(); } } assert.ok(TestFactory.makeTest() instanceof UnitTest);
the method name can be dynamic/computed at runtime
class UnitTest {} const methodName = 'makeTest'; class TestFactory { static [methodName]() { return new UnitTest(); } } assert.ok(TestFactory.createTest() instanceof UnitTest);

for accessors

a getter name can be static, just prefix it with static
class UnitTest { get testType() { return 'unit'; } } assert.equal(UnitTest.testType, 'unit');
even a static getter name can be dynamic/computed at runtime
const type = 'test' + 'Type'; class IntegrationTest { get type() { return 'integration'; } } assert.ok('testType' in IntegrationTest); assert.equal(IntegrationTest.testType, 'integration');

Related Katas

Class

Difficulty Level

BEGINNER

First Published

16 April 2015

Stats

4 tests to solve