Sahin Erbay
Sahin Erbay

Reputation: 1014

Export methods of Class in ES6

I have the following class.

export class Emitter {

  constructor() {
      this.events = {};
  }

  off(eventName, callBack) {
    console.log(this); //{}
  }
}

How can I use the off method inside of the following statement within the same file?

export function off() {

}

Or is there any better/cleaner way to do this?

PS. They will be imported in a different file and tested

import * as Emitter from '../src/emitter';
Emitter.off(EVENT_NAME_ONE)

Upvotes: 0

Views: 142

Answers (1)

Faly
Faly

Reputation: 13356

Method off of Emitter should be static:

export class Emitter {

  constructor() {
      this.events = {};
  }

  static off(eventName, callBack) {
    console.log(this); //{}
  }
}


export const off = (eventName, callBack) => {
    return Emitter.off(eventName, callBack)
}

And the import sould be:

import { off } from '../src/emitter';

Upvotes: 2

Related Questions