Calling a function when passing no parameters

Y

Yansky

If I have the following function:

function foo(){

alert('hi');

}

and I don't need to pass any parameters to it, is calling it this way:

foo;

the same as calling it this way:

foo();
 
J

Joost Diepenmaat

Yansky said:
If I have the following function:

function foo(){

alert('hi');

}

and I don't need to pass any parameters to it, is calling it this way:

foo;

the same as calling it this way:

foo();

No. foo; does not call it at all. It's a statement returning the
function foo, but since you're not doing anything with the value it's
a useless statement.

A contrived example of foo; is:

var bar = foo; // assign function foo to bar
bar(); // call the function.

IOW, you do need the parentheses if you want to call a function.
 
Y

Yansky

No. foo; does not call it at all. It's a statement returning the
function foo, but since you're not doing anything with the value it's
a useless statement.

A contrived example of foo; is:

var bar = foo; // assign function foo to bar
bar();  // call the function.

IOW, you do need the parentheses if you want to call a function.

Thanks for clearing that up. :)
 
Y

Yansky

Thanks for clearing that up. :)

Oh wait I have one more question. What about when you're calling the
function inside an event listener? e.g.
function foo(){

alert('hi');

}
window.addEventListener("load", foo, false);
 
T

Thomas 'PointedEars' Lahn

That's probably the second most stupid question I have ever read on Usenet.
No. foo; does not call it at all. It's a statement returning the
function foo, but since you're not doing anything with the value it's
a useless statement.

A contrived example of foo; is:

var bar = foo; // assign function foo to bar
bar(); // call the function.

IOW, you do need the parentheses if you want to call a function.
[...]

Thanks for clearing that up. :)

I really wonder what is so difficult about trying something out before
asking (or to read the available reference material, including this
newsgroup's FAQ, for that matter). If you are this afraid that code that
you write could have effects this dangerous on the computer you are
developing on, you should forget all about programming and computer science,
and move to a happy little island nowhere near any technology.

<http://jibbering.com/faq/>
<http://catb.org/~esr/faqs/smart-questions.html>


PointedEars, shaking his head
 
T

Thomas 'PointedEars' Lahn

Yansky said:
Oh wait I have one more question.

You don't say.
What about when you're calling the function inside an event listener?
e.g.
function foo(){

alert('hi');

}
window.addEventListener("load", foo, false);

`foo' refers to the (event) listener, which is the user-defined function.
Again, the function is _not_ called here, but being referred to. It is
called *later*, by the DOM implementation's event handler, using the passed
Function object reference.

RTFM. RTFFAQ. STFW.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Yansky said:

And that's definitely the most stupid reply I have ever read.

Congratulations. Don't you want to apply for the Darwin Award as well?


PointedEars
 
S

Stevo

Yansky said:
Oh wait I have one more question. What about when you're calling the
function inside an event listener? e.g.
function foo(){
alert('hi');
}
window.addEventListener("load", foo, false);

That's fine, you're not calling it, you're nominating it as the function
that should be called.

Here, alias will be set to 3;

function foo(){return 3;};
var alias=foo();

Here, alias will become an actual alias of the function foo.

function foo(){return 3;}
var alias=foo;
//you can now do this:
alert(alias()); //alert 3
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top