olegmil
olegmil

Reputation: 455

How can I invoke a named IIFE again by the name I gave it?

Im trying to call to self-invoking function this way:

(function fullscreen(){
  alert("test");
})();
$(window).resize(function() {
  fullscreen();
});

it works only once. no response on window.resize thanks

Upvotes: 2

Views: 160

Answers (2)

Pointy
Pointy

Reputation: 413682

When you instantiate a function using the syntax typically employed for self-invoking functions, you're creating an anonymous function even though you give it a name. In a function instantiation expression, the name you give after the function keyword is bound inside the function, and not outside.

(That's actually not true in Internet Explorer, but that's because Internet Explorer is broken.)

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359776

What you're doing does not make sense. Why is fullscreen self-invoking? Just do this:

function fullscreen() {
  alert("test");
}

fullscreen();

$(window).resize(fullscreen);
// or
$(window).resize(function () {
    fullscreen();
});

Upvotes: 3

Related Questions