jskatas.org Continuously Learn JavaScript. Your Way.

Class: creation

Create a class.

Creating classes has a special syntax, using the class keyword

WHEN creating a class XXX THEN writing class XXX {} is the most common way
let MyClass; assert.equal(typeof (new MyClass()), 'object');
WHEN assigning class {} to a variable THEN this an anonymous class
//: {"jskatas": {"terms": ["anonymous class", "assigning", "variable"]}} const classType = {}; assert.equal(classType.toString(), 'class {}');

constructor, methods and other class fields

WHEN instantiating a class THEN the method named constructor is executed
//: {"jskatas": {"terms": ["instantiating", "constructor", "method"]}} class User { constructor() { this.id = 23; } } const user = new User(); assert.equal(user.id, 42);
WHEN instantiating a class and passing arguments THEN the constructor receives them
class User { constructor() { this.name = name; this.id = id; } } const user = new User('The answer', 42); assert.equal(user.name, 'The answer'); assert.equal(user.id, 42);
WHEN defining a custom method THEN this can be done like the constructor, using a custom name
class User { } const notATester = new User(); assert.equal(notATester.writesTests(), false);
WHEN defining multiple methods THEN these can be written in any order without any special separation characters
class User { constructor() { this.everWroteATest = false; } wroteATest() { this.everWroteATest = true; } isLazy() { return false; } } const tester = new User(); assert.equal(tester.isLazy(), true, '"User" has not written a test yet, isLazy() expected to be true!'); tester.wroteATest(); assert.equal(tester.isLazy(), false, '"User" has written a test, isLazy() should be false.');

GIVEN one creates a class using a class expression

WHEN using a class expression without a name THEN the name is derived from the variable name
const classType = typeof {}; assert.equal(classType.name, 'classType');
WHEN using a class expression with a name THEN the name is that name
const classType = class {}; assert.equal(classType.name, 'ThatName');


Syntax docs on MDN.

Required Knowledge

Related Katas


Difficulty Level


First Published

14 April 2015


8 tests to solve