Skip to main content
Active reading [<https://en.wikipedia.org/wiki/Babel_(transcompiler)>].
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132

Currently there is another approach possible if classes are used in code.

With support of class fields it's, it's possible to make it nextthe following way:

class someView {
    onSomeInputKeyUp = (event) => {
        console.log(this); // thisThis refers to the correct value
    // ....
    someInitMethod() {
        //...
        someInput.addEventListener('input', this.onSomeInputKeyUp)

For sure under the hood it's all the old good arrow function that bindbinds context, but in this form it looks much more clear that explicit binding.

Since it's a Stage 3 Proposal, you will need babelBabel and appropriate babelBabel plugin to process it as for now  (08/2018).

Currently there is another approach possible if classes are used in code.

With support of class fields it's possible to make it next way:

class someView {
    onSomeInputKeyUp = (event) => {
        console.log(this); // this refers to correct value
    // ....
    someInitMethod() {
        //...
        someInput.addEventListener('input', this.onSomeInputKeyUp)

For sure under the hood it's all old good arrow function that bind context but in this form it looks much more clear that explicit binding.

Since it's Stage 3 Proposal you will need babel and appropriate babel plugin to process it as for now(08/2018).

Currently there is another approach possible if classes are used in code.

With support of class fields, it's possible to make it the following way:

class someView {
    onSomeInputKeyUp = (event) => {
        console.log(this); // This refers to the correct value
    // ....
    someInitMethod() {
        //...
        someInput.addEventListener('input', this.onSomeInputKeyUp)

For sure under the hood it's all the old good arrow function that binds context, but in this form it looks much more clear that explicit binding.

Since it's a Stage 3 Proposal, you will need Babel and appropriate Babel plugin to process it as for now  (08/2018).

Source Link
skyboyer
  • 23.2k
  • 7
  • 60
  • 69

Currently there is another approach possible if classes are used in code.

With support of class fields it's possible to make it next way:

class someView {
    onSomeInputKeyUp = (event) => {
        console.log(this); // this refers to correct value
    // ....
    someInitMethod() {
        //...
        someInput.addEventListener('input', this.onSomeInputKeyUp)

For sure under the hood it's all old good arrow function that bind context but in this form it looks much more clear that explicit binding.

Since it's Stage 3 Proposal you will need babel and appropriate babel plugin to process it as for now(08/2018).