0
a = function(x) {
    return new Date().getTime();
}

b = function(x) {
    return new Date().getTime();
}

c = function(x,y) {
    return x+' '+y
}

console.log(c(a(),b()));

In the above code, functions a and b are called simultaneously by function c. Does function a execute first and then function b, or do both functions execute simultaneously? It's hard to tell since the time stamps returned are the same.

3
  • 5
    The function c() does not call either a() or b(); they're called in preparation for the call to c(), and they're called one at a time, left to right. Commented Aug 5, 2020 at 12:56
  • 2
    They aren't called simultaneously or by c, the arguments to c are resolved left-to-right by calling a then b. Commented Aug 5, 2020 at 12:57
  • This confusion could of been solved by not writing this code in the first place c(a(),b()). If it's not clear what's happening, don't do it that way. Good programmers write code that humans can understand. Commented Aug 5, 2020 at 13:07

2 Answers 2

4

c(a(),b()) - this is a function invocation expression.

Invocation expressions consist of following parts:

  • function expression that identifies the function that is to be called.
  • opening parenthesis.
  • comma-separated list of zero or more argument expressions.
  • closing parenthesis.

When function invocation expression is evaluated, function expression is evaluated first and then the argument expressions, if there are any, are evaluated (from left to right) to produce values that are to be passed to the function that is being called.

c(a(),b()) is evaluated as:

  1. First of all, identifier c is evaluated. If c wasn't defined, javascript would have thrown a ReferenceError.

  2. Since in this case, c is a function, so argument expressions are evaluated from left to right.

  3. First a() is evaluated which itself is an invocation expression. So a is evaluated first and since there are no arguments, function a is invoked and the body of the function a executes. Since a returns new Date().getTime(), it becomes the value of a() invocation expression.

  4. After that b() is evaluated in the same way as a() is evaluated.

  5. Finally, c() is called with the return values of a() and b() as its arguments. If c wasn't a function, javascript would have thrown a TypeError.

Sign up to request clarification or add additional context in comments.

7 Comments

Actually it only fails at step 1 if c is not defined, with a ReferenceError. If c is defined, the arguments are evaluated before trying to invoke it with those values, so if it's not actually callable you only find out with a TypeError around step 5.
If you do e.g. "foo"((function () { throw new Error("whoops!"); })()) you'll see the uncaught whoops! error, not a TypeError that a string isn't callable.
@jonrsharpe that seems to verify what you are saying but it also contradicts what David Flanagan, author of Javascript: The Definitive Guide, says about Invocation Expressions in his book. If you read the fourth line, it says that function expression is evaluated before argument expressions.
No, I don't think that's contradictory. That also says that the TypeError would be after the argument values are evaluated.
@jonrsharpe Ok, so if i understand what you are saying and what david says in his book, is that TypeError is thrown only after evaluating the argument expressions but function expression itself is evaluated before argument expressions, right?
|
2

Javascript is always running in one Thread, so they are called after one another.

The reason why the timestamp is the same is because the clock of your computer (or the client computer) has only a specific resolution. I have something of 16ms in mind which is definitively bigger than the call to the two functions.

Anyway I'm not exactly sure what function is called first I would assume a and than b, but this can be dependent to the Javascript interpreter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.