• Use extends for inheritance.

    1. // bad
    2. const inherits = require('inherits');
    3. function PeekableQueue(contents) {
    4. Queue.apply(this, contents);
    5. }
    6. inherits(PeekableQueue, Queue);
    7. PeekableQueue.prototype.peek = function () {
    8. return this.queue[0];
    9. };
    10. // good
    11. class PeekableQueue extends Queue {
    12. return this.queue[0];
    13. }

  • 9.3 Methods can return this to help with method chaining.

  • It’s okay to write a custom toString() method, just make sure it works successfully and causes no side effects.

    1. class Jedi {
    2. constructor(options = {}) {
    3. this.name = options.name || 'no name';
    4. }
    5. getName() {
    6. return this.name;
    7. }
    8. toString() {
    9. return `Jedi - ${this.getName()}`;
  • 9.5 Classes have a default constructor if one is not specified. An empty constructor function or one that just delegates to a parent class is unnecessary. eslint:

  • Avoid duplicate class members. eslint: no-dupe-class-members

    Why? Duplicate class member declarations will silently prefer the last one - having duplicates is almost certainly a bug.

    1. // bad
    2. class Foo {
    3. bar() { return 1; }
    4. bar() { return 2; }
    5. }
    6. // good
    7. class Foo {
    8. bar() { return 1; }
    9. }
    10. // good
    11. class Foo {
    12. }