1

In the book "Head First Javascript" on pages 464 and 465 there are two statements that are contradicting.

p 464

When the browser evaluates a function declaration, it creates a function as well as a variable with the same name as the function, and stores the function reference in the variable.

p 465

A function declaration doesn't return a reference to a function; rather it creates a variable with the name of the function and assigns the new function to it.

So which one is it? When the interpreter looks at a function declaration, creates a variable- does it assign a function or a function reference to that variable?

Is the statement "A function declaration doesn't return a reference to a function" just wrong?

2
  • Hi, I've removed the tags [java] and [c++] since you don't mention them in your question. Commented Oct 8, 2016 at 16:36
  • 1
    See -> jsfiddle.net/2vz577n2/1 Commented Oct 8, 2016 at 16:39

3 Answers 3

2

JavaScript doesn't really distinguish between the two.

Any time you have a value which is an object, the value is really a reference to that object.

(Keeping in mind that functions are just a type of object in JS).

Function declarations can't return anything, they can't exist anywhere that has a left-hand-side to return to (if they did, then they would be a function expression and not a declaration). They create a variable with the same name as them in the current scope.

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

2 Comments

So, is the statement "A function declaration doesn't return a reference to a function" just wrong?
No, it doesn't have to return anything, as then it would be an expression. It does however create a variable with the name given to the function, and assigns a reference to that variable, and that's why you can call the function later
1

They are both saying the same thing, in a bit of a different way.

I think you're assuming the top statement is saying the function returns a reference.

Read the statements like this, with the omitted portion:

p 464

When the browser evaluates a function declaration, it creates a function as well as a variable with the same name as the function, and stores the function reference in the variable.

p 465

A function declaration doesn't return a reference to a function; rather it creates a variable with the name of the function and assigns the new function to it.

Now, you can add the omitted portion of it to either statement. You'll see that they are saying the same thing, but the second one is just clarifying that the function declaration does not return a reference to a function. The first statement does not say it does return something, so this is not contradictory.

The first statement says it stores the function reference in the variable created with the same name as the function. Do not assume this means the function declaration returns something.

It seems there is some confusion between declaration and invocation. Here's a very simple definition of both.

Declaration

The act of defining what something is, such as a variable, object, method, or function. In JavaScript, declaring a function does not return anything.

function func() {
    return "returned";
}

You can also do this

var funk = function func() {
    return "returned";
}

This doesn't mean the declaration returns something, however. It simply means that funk is now pointing to the reference to the function. Later you can invoke this function by using funk(), similar to below.

Invocation

The act of invoking, or running, a function or method.

func();

The above returns "returned", as defined in the declaration. In JavaScript, the declaration of the function itself doesn't return anything.

4 Comments

So, if the function declaration has a variable and that variable contains a reference to the function, then can't you say that it DOES return a reference to a function? It contains the reference, right?
So, the word "return" in this context is specifically referring to the keyword "return"? not synonymous with contains
The function declaration will not return the reference. I think you're confused with the term declaration. The function hasn't been invoked at this point. The declaration doesn't return anything. If you invoke the function, sure, you can return something, but this is not talking about invocation, it is talking about declaration.
I updated my response. Let me know if you have any other questions. Here's some good reading - javascriptweblog.wordpress.com/2010/07/06/…
0

Objects are "reference values", and functions are objects. We usually mean the same when saying "object" or "reference to object". It's the same with functions.

Is the statement "A function declaration doesn't return a reference to a function" just wrong?

No, it's correct. But there is no distinction between "function" or "reference to a function" here, the emphasis is on doesn't return. A declaration is a statement, not an expression, so it has no result value.

2 Comments

So, function expressions always return the reference to the function?
Yes, a function expression results in a reference to the new function

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.