0

I have this simple node module

module.exports = {
  func1: () => {
  }, 
  func2: () => {

  }
}

Now when I wanted to refer to func1 inside func2 I used to do this.func1 before ES6

So now as the this is different in ES6. Is it correct that I have to do module.exports.func1 inside func2?

So, it's going to be?

func2: () => {
  module.exports.func1();
}
toy
  • 11,711
  • 24
  • 93
  • 176
  • why don't you declare them in separated variables? then just export them in that object – NaN Mar 30 '16 at 18:52
  • Just don't use arrow functions here. Use a method declaration instead. – Bergi Mar 30 '16 at 18:55
  • `export const a = () => 'a'; export const b = () => a();` – Mulan Mar 30 '16 at 18:55
  • 1
    Yes, `module.exports.func1` will work as expect. Though actually you just should use ES6 modules, have two named exports, and wouldn't need to refer to them as properties at all. – Bergi Mar 30 '16 at 18:56
  • @Bergi I think that might be the wrong question to dupe as. It doesn't seem to talk about exports, just the semantics of arrow functions. – ssube Mar 30 '16 at 18:56
  • @ssube: Actually I think this question does only ask about arrow functions, and doesn't mention modules at all. You could easily replace `module.exports` with an arbitrary variable and the question wouldn't really change. If we were at [CodeReview.SE] it might be different, but that's why I've mentioned the proper module exports only in the comments. – Bergi Mar 30 '16 at 19:01

1 Answers1

1

As @NaN said in the comments, you'd be better off declaring these separately and exporting them later:

export function func1() {
  ...
}

export function func2() {
  ...
}

// or if you need to rename them:
export default {
  funcA: func1,
  funcB: func2
};
Community
  • 1
  • 1
ssube
  • 47,010
  • 7
  • 103
  • 140