Alicia Brandon
Alicia Brandon

Reputation: 565

can't call anonymously function again in javascript?

var func = (function(){
//do something
})();

and then I call func() it says func is not a function?

if that's the case I have to do this

function func(){
//do something
};
func();

and later

func();

correct?

Upvotes: 0

Views: 120

Answers (6)

Rajesh
Rajesh

Reputation: 24945

Following syntax

(function(){
  // Do something
})()

Is called Immediately Invoked Function Expression.

When you do

var func = (function(){
//do something
})();

You not assigning a function but its response to it. So if you return an object, it will have reference of that object and not the function.

If you wish to have a function to act as an init and being able to call it after words, have a named function and call this function,

function init(){}
init();

Refer What is the (function() { } )() construct in JavaScript? for more reference.

Also when you define a function like

function  func1(){}
// or
var func1 = function(){}

an reference is created in memory and is assigned to functionName, and later this name is used to call it. When you create an anonymous function, you are not storing any reference and hence you cannot call it.

Upvotes: 0

Scott Hunter
Scott Hunter

Reputation: 49896

The error is correct; func is the result of calling your function (caused by the () after the definition). Leaving that off would fix this.

Note that you cannot re-use an anonymous function, since you can't identify the function to be re-used. Since you are assigning it to a variable, you are effectively naming it.

Upvotes: 1

user123123123
user123123123

Reputation: 318

A Self-Invoking Anonymous Function is a function that you can execute only one time because it has no reference.

In this case

var func = (function(){
    //do something
})();

the variable func has no reference to the anonymous function, then it will contain the value returned by the function, in this case undefined.

If you want to be able to call the function you have to create a reference like this:

var func = function(){
    //do something
};

or

function func() { 
    // do something
}

Upvotes: 1

Abdelrahman M. Allam
Abdelrahman M. Allam

Reputation: 922

Function you use here is already executed to variable func with (function(){})()

to use func as function you have to return function

var func = (function(){
  return function (){ 
     //  do something here
  }
})();

so you can call func()

jsfiddle example

Upvotes: 1

Dest
Dest

Reputation: 728

Just make like this you don't need () <- this in last line

 var func = function(){

    }
console.log(func);

Upvotes: 0

melpomene
melpomene

Reputation: 85877

var func = (function(){
//do something
})();

This code has () at the end, i.e. it's already calling the function. The value assigned to func is the return value of the function.

If you want to assign the function itself to func, just do:

var func = function () {
  //do something
};

Upvotes: 6

Related Questions