seventeen
seventeen

Reputation: 443

Javascript: IIFE, also calling it later

I have a IIFE

(function test() {
  console.log("test called!");
})();

Does some way exist that I can call test later on?

Upvotes: 0

Views: 442

Answers (1)

Sagar V
Sagar V

Reputation: 12478

IIFE gets invoked immediately and returns the result. You can't call it later.

If you want to call it later, create a normal function.

However if you really want to do this, there's a workaround. Create a global variable and assign the function to it. BUT I recommend creating a regular function for this purpose.

(function test() {
  window.testFn = test;
  console.log("test called!");
})();

testFn();

Upvotes: 1

Related Questions