JavaScript


Classes 6

1
2
3
E4X
5
5.1
6
7
8

This draft deletes the entire topic.

Introduction

Introduction

expand all collapse all

Examples

  • 16

    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.

    class MyClass {
        constructor(option) {
            console.log(`Creating instance using ${option} option.`);
            this.option = option;
        }
    }
    

    Example usage:

    const foo = new MyClass('speedy'); // logs: "Creating instance using speedy option"
    

    A small thing to note is that a class constructor cannot be made static via the static keyword, as described below for other methods.

  • 13

    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 {
    
        constructor() {
            this.logger = console.log;
        }
    
        log() {
            this.logger(`Hello ${this.name}`);
        }
    
    }
    
    class SubClass extends SuperClass {
    
        constructor() {
            super();
            this.name = 'subclass';
        }
    
    }
    
    const subClass = new SubClass();
    
    subClass.log(); // logs: "Hello subclass"
    
  • 10

    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() {
            return 'Goodbye';
        }
    }
    
    console.log(MyClass.myStaticMethod()); // logs: "Hello"
    console.log(MyClass.myStaticProperty); // logs: "Goodbye"
    

    We can see that static properties are not defined on object instances:

    const myClassInstance = new MyClass();
    
    console.log(myClassInstance.myStaticProperty); // logs: undefined
    

    However, they are defined on subclasses:

    class MySubClass extends MyClass {};
    
    console.log(MySubClass.myStaticMethod()); // logs: "Hello"
    console.log(MySubClass.myStaticProperty); // logs: "Goodbye"
    
Please consider making a request to improve this example.

Syntax

  • class Foo {}
  • class Foo extends Bar {}
  • class Foo { constructor() {} }
  • class Foo { myMethod() {} }
  • class Foo { get myProperty() {} }
  • class Foo { set myProperty(newValue) {} }
  • class Foo { static myStaticMethod() {} }
  • class Foo { static get myStaticProperty() {} }
  • const Foo = class Foo {};
  • const Foo = class {};

Parameters

Parameters

Remarks

class support was only added to JavaScript as part of the 2015 standard.

Javascript classes are syntactical sugar over JavaScript's already existing prototype-based inheritance. This new syntax does not introduce a new object-oriented inheritance model to JavaScript, just a simpler way to deal with objects and inheritance. A class declaration is essentially a shorthand for manually defining a constructor function and adding properties to the prototype of the constructor. An important difference is that functions can be called directly (without the new keyword), whereas a class called directly will throw an exception.

class someClass {
    constructor () {}
    someMethod () {}
}
 
console.log(typeof someClass);               
console.log(someClass);
console.log(someClass === someClass.prototype.constructor);                         
console.log(someClass.prototype.someMethod);
 
// Output:
// function
// function someClass() { "use strict"; }
// true
// function () { "use strict"; }

If you are using an earlier version of JavaScript you will need a transpiler like or in order to compile the code into a version that the target platform will be able to understand.

Still have a question about Classes? Ask Question

Topic Outline