-2

I have created one common util file to write some common foundations like below.

export default { 
    a: () => {
        console.log("Hello");
    }

    b: () => {
        this.a();
    }
}

I want to call the function into the function b. I have tried lots of things but none of them are working. Please help me in this concern.

Rav's Patel
  • 663
  • 5
  • 19
  • 2
    With an arrow function the `this` will not be respecting to that object but to the context above. The arrow functions bubble the context up. Use the function key word instead and it will work. – Rafael Rocha Jun 15 '20 at 07:54

3 Answers3

0

You can definetly do this.

var xxx = { 
    a: () => {
        console.log("Hello");
    },

    b: () => {
        xxx.a();
    }
}
xxx.b();

But i dont know why you want it like that.

Jovylle Bermudez
  • 644
  • 2
  • 13
0

As you want export this util functionality, so write the following code in util.js file.

export default class Util{
    a = () => {
        console.log("Hello");
    };

    b = () => {
        this.a();
    }
}

and where you want to import this code, you can use as below:

import Util from './util.js'
Khabir
  • 3,147
  • 1
  • 11
  • 18
0

You should try something like:

    const a = () => {
      console.log("Hey it's A");
    }

    const b = () => {
      a();
      console.log("Hey it's B");
    }

    export { a, b };
Jon
  • 166
  • 6