(simple) question - function calls without ()

E

ed

Hi there,

I just remembered this from a few days ago, and was looking for a
reason for it. I was trying to call a function as so:

addEvent( whatever, whatever, functionName() );

and as I recall it didn't like the parenthesis (). I got rid of them,
and it worked. What is the reasoning behind this? When are you
supposed to add the () ?

Thanks!

eddie
 
R

Randy Webb

ed said the following on 6/17/2007 10:15 AM:
Hi there,

I just remembered this from a few days ago, and was looking for a reason
for it. I was trying to call a function as so:

addEvent( whatever, whatever, functionName() );

and as I recall it didn't like the parenthesis (). I got rid of them,
and it worked. What is the reasoning behind this? When are you
supposed to add the () ?

When you want the function executed right then. Or, the function you are
calling expects the () to be there. With the (), the function executes
then and what your above function call would end up as would be
something like this:

addEvent( whatever, whatever, returnFromfunctionName );

Without the () you are simply passing the name of a function.
 
L

-Lost

Randy said:
ed said the following on 6/17/2007 10:15 AM:

When you want the function executed right then.

Or, the function you are calling expects the () to be there.

I don't understand this, could you explain this a little further? Or
give me an example?

I have no clue how the function would "expect" it to be there, or even
be able to tell "how" it was called.
With the (), the function executes
then and what your above function call would end up as would be
something like this:

addEvent( whatever, whatever, returnFromfunctionName );

Without the () you are simply passing the name of a function.

Thank you (in advance)!
 
D

Douglas Crockford

Randy said:
ed said the following on 6/17/2007 10:15 AM:

When you want the function executed right then. Or, the function you are
calling expects the () to be there. With the (), the function executes
then and what your above function call would end up as would be
something like this:

addEvent( whatever, whatever, returnFromfunctionName );

Without the () you are simply passing the name of a function.

Not the name of the function, but the function itself.
 
P

Peter Michaux

I don't understand this, could you explain this a little further? Or
give me an example?

I second this request (but I'm guessing it was loose wording.)

Peter
 
R

Randy Webb

Douglas Crockford said the following on 6/17/2007 1:23 PM:
Not the name of the function, but the function itself.

That would depend on how it was called and what was expected. I don't
think that setTimeout (or addEvent) is getting passed the entire
function, just a name reference to it.
 
R

Randy Webb

-Lost said the following on 6/17/2007 12:44 PM:
I don't understand this, could you explain this a little further? Or
give me an example?

I have no clue how the function would "expect" it to be there, or even
be able to tell "how" it was called.

Think about setTimeout and this thread:

<URL:
http://groups.google.com/group/comp...settimeout+randy+webb&rnum=8#76177fa1a14eb9a1>
Make sure you read the last post by me in that thread as my first post
with 4 functions had an error in it that is covered in the very last post.

A call to setTimeout wants () in some instances and not in others.
 
P

Peter Michaux

-Lost said the following on 6/17/2007 12:44 PM:






Think about setTimeout and this thread:

<URL:http://groups.google.com/group/comp.lang.javascript/browse_frm/thread...>
Make sure you read the last post by me in that thread as my first post
with 4 functions had an error in it that is covered in the very last post.

A call to setTimeout wants () in some instances and not in others.


A slightly odd example since the () are inside a string so setTimeout
doesn't actually see the (). It just sees a string that will be
eval'ed later. Calling setTimeout with a string is akin to eval()
which we all know is a sin.

Peter
 
P

Peter Michaux

Douglas Crockford said the following on 6/17/2007 1:23 PM:



That would depend on how it was called and what was expected. I don't
think that setTimeout (or addEvent) is getting passed the entire
function, just a name reference to it.

eh?

var foo = function() {
console.log('foo');
};
setTimeout(foo,10);
foo = function() {
console.log('foobar');
};

The output of the above is "foo" because setTimeout is passed the
first function (not it's name)


var foo = function() {
console.log('foo');
};
setTimeout("foo()",10);
foo = function() {
console.log('foobar');
};

The output of the above is "foobar" because the string "foo()" is
evaluated when the timeout expires. However setTimeout is being passed
a string in this case and knows nothing about the ()

Anyway, I think it would be better to write this example as.

var foo = function() {
console.log('foo');
};
setTimeout(function(){foo();},10);
foo = function() {
console.log('foobar');
};

which outputs "foobar" since foo has been redefined by the time the
timeout expires.

Peter
 
E

ed

eh?

var foo = function() {
console.log('foo');
};
setTimeout(foo,10);
foo = function() {
console.log('foobar');
};

The output of the above is "foo" because setTimeout is passed the
first function (not it's name)


var foo = function() {
console.log('foo');
};
setTimeout("foo()",10);
foo = function() {
console.log('foobar');
};

The output of the above is "foobar" because the string "foo()" is
evaluated when the timeout expires. However setTimeout is being passed
a string in this case and knows nothing about the ()

Anyway, I think it would be better to write this example as.

var foo = function() {
console.log('foo');
};I
setTimeout(function(){foo();},10);
foo = function() {
console.log('foobar');
};

which outputs "foobar" since foo has been redefined by the time the
timeout expires.

Peter

Wow, that's really interesting. Thanks for the explanation. One
question; in the secont mini-example, what causes the string "foo()"
to be evaluated? Is that a specification of setTimeout, or a result of
some other language concept? I haven't yet seen string evaluation in
my studies.
-e
 
P

Peter Michaux

Wow, that's really interesting. Thanks for the explanation. One
question; in the secont mini-example, what causes the string "foo()"
to be evaluated? Is that a specification of setTimeout, or a result of
some other language concept? I haven't yet seen string evaluation in
my studies.

setTimeout can take a string or function as it's first argument. I
believe the string was first and the function option was added later.
Anyway, it must be setTimeout that actually calls eval if the argument
was a string when the timeout expires.

<URL: http://developer.mozilla.org/en/docs/DOM:window.setTimeout>

This isn't really a language-related issue. The setTimeout function is
not part of the actual language and so not in ECMAScript
specification. This function is a property of the window object that
the browser provides and that your code can use. You can write
window.setTimeout() to specifically show that you are calling a
property of the window object.

Peter
 
D

Dr J R Stockton

In comp.lang.javascript message said:
Hi there,

I just remembered this from a few days ago, and was looking for a
reason for it. I was trying to call a function as so:

addEvent( whatever, whatever, functionName() );

and as I recall it didn't like the parenthesis (). I got rid of them,
and it worked. What is the reasoning behind this? When are you
supposed to add the () ?

Your functionName is, presumably, the name of a function, and a function
is a "recipe" for possible action. There is a clear distinction in
cooking between a recipe, which can be published, and the result of
executing it, which can be eaten.

Passing functionName is equivalent to passing "Cake recipe book page
35". Passing functionName() is equivalent to passing a cake resulting
from using page 35.

The parentheses () - which need not be contiguous - are an operator
which, when postfixed to a function identification (name, expression,
etc.), cause the function to be executed - the function is given the
contents of the parenthesis pair as data or, for cake, variable
ingredients.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
L

-Lost

Dr said:
Your functionName is, presumably, the name of a function, and a function
is a "recipe" for possible action. There is a clear distinction in
cooking between a recipe, which can be published, and the result of
executing it, which can be eaten.

Passing functionName is equivalent to passing "Cake recipe book page
35". Passing functionName() is equivalent to passing a cake resulting
from using page 35.

The parentheses () - which need not be contiguous - are an operator
which, when postfixed to a function identification (name, expression,
etc.), cause the function to be executed - the function is given the
contents of the parenthesis pair as data or, for cake, variable
ingredients.

I am confused. Did you just bake a cake or answer the originally posted
question?
It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

Hrmmm... I checked, and it says nothing about cakes, recipes, or
ingredients.

Perhaps you could elaborate without the use of your "bakery?" :D
 

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,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top