Skip to content

Latest commit

 

History

History
46 lines (33 loc) · 927 Bytes

File metadata and controls

46 lines (33 loc) · 927 Bytes

JavaScript - Use public class fields syntax

Use the constructor only when necessary,
otherwise define public properties and methods in the class body.

  • Reduce amount of code
  • Autobinding

React doc - Handling events
React doc - Autobinding

❌ BAD
class Basket {
    constructor() {
        this.count = 0;
        this.handleAdd = this.handleAdd.bind(this);
    }

    handleAdd({add}) {
        this.count += add;
    }
}
✔ GOOD
class Basket {
    count = 0;

    handleAdd = ({add}) => {
        this.count += add;
    };
}

Back to Code Guide - JavaScript

Back to Code Guide - Readme


Copyright © 2017 Stanislav Kochenkov