jskatas.org Continuously Learn JavaScript. Your Way.

Class: creation

Create a class.

Class creation

is as simple as class XXX {}
let TestClass; const instance = new TestClass(); assert.equal(typeof instance, 'object');
a class is block scoped
class Inside {} {class Inside {}} assert.equal(typeof Inside, 'undefined');
the constructor is a special method
class User { constructor(id) {} } const user = new User(42); assert.equal(user.id, 42);
defining a method by writing it inside the class body
class User { } const notATester = new User(); assert.equal(notATester.writesTests(), false);
multiple methods need no commas (opposed to object notation)
class User { wroteATest() { this.everWroteATest = true; } isLazy() { } } const tester = new User(); assert.equal(tester.isLazy(), true); tester.wroteATest(); assert.equal(tester.isLazy(), false);
anonymous class
const classType = typeof {}; assert.equal(classType, 'function');