-4

I am just starting to learn javascript while reading a book about node.js. In one of the first examples it shows a high-order function:

setTimeout(function () {
    console.log('2000 milliseconds have passed since this demo started');
}, 2000);

This works when I run it in the REPL. It waits 2 seconds and then writes the string I gave it.

So I tried doing something that I expected would produce the same output:

setTimeout(console.log('2000 milliseconds have passed since this demo started'),2000);

This printed the string out immediately. I am guessing that the setTimeout still waited and then did nothing at the end of the 2 seconds. It just printed the string out first for some reason.

I played around with it a little more and created another function that just prints a string:

function tester() {
    console.log('tester function ran');
}

I then called this function inside of setTimeout:

setTimeout(tester(),2000);

It still printed the string in tester() out first and then waited 2 seconds and did nothing else.

I then tried removing the parenthesis:

setTimeout(tester,2000);

This "worked". It waited 2 seconds and then printed out the string in tester().

My question now is what is the difference between tester() and tester in this context? And does this mean that I can't pass arguments to a function inside of the high-order function setTimeout()? If so, why?

svenwinkle
  • 1,927
  • 2
  • 10
  • 8
  • When you are using foo() - You are invoking the method, when you are using just foo, you are passing a reference to a method that will be invoked later on. ( When the delay you set has finished ) – elad.chen Aug 17 '16 at 18:36
  • ... one calls the function, the other doesn't.... and yes, you can pass arguments to a function inside of setTimeout, just not in the way you might expect. – Kevin B Aug 17 '16 at 18:36

3 Answers3

1

My question now is what is the difference between tester() and tester in this context?

foo evaluates as a function.

foo() calls the function (with no arguments) and evaluates as its return value.

And does this mean that I can't pass arguments to a function inside of the high-order function setTimeout()?

No. setTimeout, in the implementation you find in browsers at least, allows you to specify arguments to be passed to the function in the third, fourth, etc arguments of the call to setTimeout. You can also create a new function which does nothing except call the existing function with specified arguments. The bind method makes this easy.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

foo is a reference to the function itself, foo() calls the function, being whatever the function returns.

ASDFGerte
  • 4,695
  • 6
  • 16
  • 33
-2

in case of setTimeout(tester(),2000); you are actually calling the method tester. And since it does not have any callback, it would not do anything.

setTimeout expects a callback method as a first parameter and that's what is getting passed when you call setTimeout(tester,2000);.

Essentially, you are referring to method definition when you just specify name. However tester() is method call.

see the below style of defining method. That's way it's clearer that tester is actually method definition:

var tester = function(){
}

Hope this helps.

Mahesh Chavda
  • 593
  • 3
  • 9