Tutorial by Examples

The fundamental part of most classes is its constructor, which sets up each instance's initial state and handles any parameters that were passed when calling new. It's defined in a class block as though you're defining a method named constructor, though it's actually handled as a special case. cla...
Static methods and properties are defined on the class/constructor itself, not on instance objects. These are specified in a class definition by using the static keyword. class MyClass { static myStaticMethod() { return 'Hello'; } static get myStaticProperty() { r...
Getters and setters allow you to define custom behaviour for reading and writing a given property on your class. To the user, they appear the same as any typical property. However, internally a custom function you provide is used to determine the value when the property is accessed (the getter), and...
Inheritance works just like it does in other object-oriented languages: methods defined on the superclass are accessible in the extending subclass. If the subclass declares its own constructor then it must invoke the parents constructor via super() before it can access this. class SuperClass { ...
JavaScript does not technically support private members as a language feature. Privacy - described by Douglas Crockford - gets emulated instead via closures (preserved function scope) that will be generated each with every instantiation call of a constructor function. The Queue example demonstra...
There is also the ability to evaluate expressions when naming methods similar to how you can access an objects' properties with []. This can be useful for having dynamic property names, however is often used in conjunction with Symbols. let METADATA = Symbol('metadata'); class Car { construct...
Methods can be defined in classes to perform a function and optionally return a result. They can receive arguments from the caller. class Something { constructor(data) { this.data = data } doSomething(text) { return { data: this.data, te...
One of the most common obstacles using classes is finding the proper approach to handle private states. There are 4 common solutions for handling private states: Using Symbols Symbols are new primitive type introduced on in ES2015, as defined at MDN A symbol is a unique and immutable data typ...
ClassDeclaration's Name is bound in different ways in different scopes - The scope in which the class is defined - let binding The scope of the class itself - within { and } in class {} - const binding class Foo { // Foo inside this block is a const binding } // Foo here is a let binding...

Page 1 of 1