Les
Les

Reputation: 547

Javascript function invocation syntax unclear

I can't seem to find a reference that describes the following syntax:

 func() ();

This invocation of func is at the end of a constructor. Other invocations within the constructor are 'normal'.

Upvotes: 1

Views: 51

Answers (3)

Tech Tech
Tech Tech

Reputation: 354

To avoid any confusion: We know that functions get invoked like this:

functionName ();

So anything before the () is the function name,

In the case of func() () put in mind that the func() is replacing the functionName, so this is also a function call, but even func() itself is a function, so we conclude that this is a function withing a function.

And for a more appropriate definition: func() () is a function that has another function as its return value, and by doing this we are calling that returned function to be executed.

Upvotes: 0

Barmar
Barmar

Reputation: 780879

func()();

is equivalent to:

var tempfunc = func();
tempfunc();

This is used for running a function that returns another function.

Upvotes: 1

user229044
user229044

Reputation: 239270

func() returns a function, which is then invoked by the second set of ().

function func () {
  return function () {
    alert("ok!");
  }
}

func()(); // ok!

Upvotes: 3

Related Questions