0

I'm working through examples in the ebook "eloquent javascript." On page 51, in the Functions chapter, in the Closure section, there's the following example:

function wrapValue2(n) {

  var localVariable = n; 
    return function() {
       return localVariable; 
  };
}

 var wrap2 = wrapValue2(3);
   console.log(wrap2());

As you can see in the last line of the code above, "console.log(wrap2());," the variable is being called inside of console.log. I was having some trouble re-writing this code until I realized there are parenthesis after the variable! Why would I put parenthesis after a variable?

Don't I only do that after a function?

Is this command using the variable as a function? If so, why?

Is it because the object contained within the variable is a function? That doesn't seem very likely.

Thanks in advance!

7
  • you only put parenthesis after a function. Commented Feb 19, 2015 at 19:38
  • wrap2 is not a mere variable, it's a function. Commented Feb 19, 2015 at 19:38
  • wrapValue2(n) returns a function as well, not a value. so you can do wrap2() Commented Feb 19, 2015 at 19:39
  • What do you think return function means? Commented Feb 19, 2015 at 19:39
  • It's a functional programming example. wrapValue2 returns a function Commented Feb 19, 2015 at 19:39

3 Answers 3

2

Exactly: those parentheses are used to call wrap2, because it's a function.

You use

var wrap2 = wrapValue2(3);

And the function wrapValue2 returns another function.

So wrap2 is a function.

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

3 Comments

so when I want to call a variable containing a function I must terminate it with "()" this means that javascript treats variables containing functions as though they are functions themselves, correct? Thanks for confirming that for me.
@AndÚ If you meant if a function declaration and a function expression are same: yes, the behave almost identically (the only difference is that function expressions are hoisted to the top of the scope). See var functionName = function() {} vs function functionName() {}.
I started looking through the many tutorials offered on the thread you linked to in the comment above, useful! Though while reading through the 4th of example of the "Function As Object" section of permandi tutorial I noticed yet another nuance that requires clarification. var ptr=myFunction"A function is referenced inside of a variable without its parenthesis! So, is the rule, then, I use parenthesis when I call a function-containing variable inside of a command, but no parenthesis are needed when defining a function-containing variable?
0

The function ...

function wrapValue2(n) {
  var localVariable = n; 
    return function() {
       return localVariable; 
  };
}

returns a function (see return function()) ... that will need executed to get the answer.

Comments

0

Because wrap2 will be function as at the end wrapValue2 returns a function not a value.

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.