1

I've been learning some React, and I have been using ES6 classes to write my class-based components. I'm working on a little Node project, and none of this syntax is working.

class Handler {
  handleReq = () => {
    this.ctx = ctx;
  };

  testFunc = async () => {

  };
}

export default (HandleReq = Handler.prototype.handleReq);

What is wrong with this syntax? Does it not run in Node? I had to install esm to get the import/export syntax working correctly, but this still fails to compile.

8
  • 5
    Class properties are not yet valid ES, they are in the proposal phase. You have to use a transpiler lile BabelJS to work with them today., Commented Mar 2, 2019 at 19:38
  • gotcha, what do people typically do these days to get this sort of thing? Do they write older styled code? Commented Mar 2, 2019 at 19:39
  • 2
    And Handler.prototype.handleReq will never work, as handleReq has to be bound to an instance, therefore it will only exist on an instance ((new Handler).handleReq) Commented Mar 2, 2019 at 19:40
  • 1
    @JohnLippson Yes, they just use normal class methods or put the arrow functions in the constructor where they belong. You don't seem to have posted your complete code, but I doubt you need arrow functions here at all. Commented Mar 2, 2019 at 19:47
  • 3
    What is export default (HandleReq = Handler.prototype.handleReq); supposed to accomplish? Apart from throwing an exception on assignment of an undeclared global variable, why are you even using a class if all you ever export is a single method? Commented Mar 2, 2019 at 19:48

2 Answers 2

2

Class Fields are still in the proposal phase (stage 3 already, so they will become part of the language soon). That means that some runtimes might support them already, however they don't have to yet. To use proposals today reliably you have to transpile it down with BabelJS.

That would transpile your code to the following ES6:

 class Handler {
  constructor() {
    this.handleReq = () => {
      this.ctx = ctx;
    };

    this.testFunc = async () => {
    };
   }
 }

therefore these methods actually only exist on an instance after construction and not on Handler.prototype.

Sign up to request clarification or add additional context in comments.

Comments

0

As others have pointed out, class fields aren't yet part of ES6 syntax without transpiling. If you want to avoid a build step, the equivalent node syntax is:

// import someImport from './some-import' is invalid, instead use:
const someImport = require('./some-import');

class Handler {
  constructor() {
    this.handleReq = this.handleReq.bind(this);
    this.testFunc = this.testFunc.bind(this);
  }

  handleReq() {
    this.ctx = ctx; // Where is ctx coming from?
  }

  async testFunc() {

  }
}

// export default Handler.prototype.handleReq is invalid, instead use:
module.exports = Handler.prototype.handleReq;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.