Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Foundations Functions Anonymous Functions

Why should we need to make a function call act like a mathematical expression at 13:54 ?

Why should we make a function act like a mathematical expression? And what is 'mathematical expression' means?

Thank you so much! this course is tough!

================================

If we surround these in parentheses, 13:47 parentheses here are not acting as a function call. 13:50 Rather, as parentheses would act in a mathematical expression, 13:54 parentheses around a value will just evaluate to that value. 13:59 So now right here these parentheses evaluate to a function object. 14:03 And since we have a function object, we can call it immediately by using parentheses again.

1 Answer

Self-executing functions (also known as Immediately Invoked Function Expressions or IIFEs) are very common when writing a library that others will use. You might want to use a bunch of variables in the creation of some object (like $ for jQuery or _ for Underscore), but only put that one object in the library user's global scope.

If you are writing code for, say, a personal website, you probably don't need to worry too much about variable polution for a long time. But you will see IIFEs if you look at library code, so it's good to know what you're looking at.

And to clear up a point of confusion here, anonymous functions may absolutely be called multiple times, but IIFEs, a special form of anonymous function, may not, since they are never assigned to a variable. Here, "anonymous" just means that the actual function object has an empty .name property, which is different from there being no variable that refers to the function object. The variable is used to access the function, while the .name is just for debugging. This is useful, because the function object has no way of knowing what variables it might be assigned to. To illustrate this difference, the following function has a name that is different from it's variable:

var functionVariable = function functionName(){}
console.log(functionVariable.name); //prints "functionName"

EDIT: please read the comments below to see why I might be wrong

I revoked the best answer that was given to me and gave it to you. I am sorry that I caused confusion here. I appreciate when I'm proved wrong, and you have definitely done that.

That's nice of you, but it happens that I just realized some mistakes I made in my own answer. For instance, I thought the .name property was the only effect of using the named form function myFunction(){...}, but then I came upon this post which used the following examples:

var z = sum(2, 3);
function sum(x, y) {
    return x+y;
}
var z = sum(2, 3);
var sum = function(x, y) {
    return x+y;
}

He explained that in the first example, sum is hoisted to the top of the scope, so z successfully gets the value 5. And in the second example, sum is not hoisted, so an error is thrown. I tried it myself, and he is correct. If I was wrong about that, I was probably wrong about other things.

Just goes to show you that just because someone says something on the internet and sounds smart, it doesn't mean they are right. We were both wrong :)

Well, we learn something new everyday, and since we aren't robots, we can't be right all the time. ;)