Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call function within same module

Tags:

javascript

My code:

export default (function () {
  (...)
  return {
    open: () => {
      (...)
    },
    close: () => {
      (...)
    },
    get: () => {
      (...)
    }
  }
})();

I wanna call the close() in get() function like this :

get: () => {
   close();
}

I tried to use this but it doesn't work.

Please give me some advice.

Thank you in advance.

like image 627
kyun Avatar asked Dec 23 '18 13:12

kyun


People also ask

Can a function called from within the same function?

Calling a function inside of itself is called recursion. It's a technique used for many applications, like in printing out the fibonacci series.

Can a function call another function in the same class?

Calling Function From another Function within Same class – In the below example, the class method Function1 calls method Function2 from the class.

Can you call the same function within a function Python?

Python also accepts function recursion, which means a defined function can call itself. Recursion is a common mathematical and programming concept. It means that a function calls itself. This has the benefit of meaning that you can loop through data to reach a result.


1 Answers

Either use method properties instead (for which this rules will work just like with standard non-arrow functions):

export default (function () {
  (...)
  return {
    open() {
      (...)
    },
    close(){
      (...)
    },
    get() {
      (...)
      this.close();
    }
  }
})();

Or define all functions that you want to be able to cross-reference before the return statement:

export default (function () {
  (...)

  const close = () => {
    (...)
  };
  return {
    open: () => {
      (...)
    },
    close,
    get: () => {
      (...)
      close();
    }
  }
})();
like image 151
CertainPerformance Avatar answered Oct 12 '22 08:10

CertainPerformance