1

Sorry if its being a noob question, but I still can't understand the difference of its usage so I am unable to search on google with right keywords to search for.

I am following this tutorial on Closures.

Now my query is for this code -

function makeFunc() {
  var name = "Mozilla";
  function displayName() {
    alert(name);
  }
  return displayName;
}

var myFunc = makeFunc();
myFunc();

Why displayName being a function is referenced as a property in the code ? Do being a closure function it needs to be returned as a property as I tried this code -

function makeFunc() {
  var name = "Mozilla";
  function displayName() {
    alert(name);
  }
  return displayName();
}

var myFunc = makeFunc();
myFunc();

Output - An alert but an error TypeError: myFunc is not a function.

Let me know when to use what or what basic concept I am missing here.

4 Answers 4

3

"return displayName;" is returning the function displayName.
The result is then being set to myFunc.

function makeFunc() {
  var name = "Mozilla";
  function displayName() {
    alert(name);
  }
  return displayName;
}

var myFunc = makeFunc();
myFunc();

It is easier to understand if you think of:
function displayName(){}
as an alternative to: displayName = function(){}

makeFunc = function () {
  var name = "Mozilla";
  displayName = function () {
    alert(name);
  }
  return displayName;
}

var myFunc = makeFunc();
myFunc();
Sign up to request clarification or add additional context in comments.

Comments

1

In first case, you're returning a function reference which you can use later. In the second case, you are returning the result of the function which is undefined since you're not returning anything. So the error comes up as myFunc is not a reference to a function and hence cannot be called.

Other than the above, I don't know what you're meaning to say when you say "property".

3 Comments

Thx for the answer..in which use case we need to return a function reference ?
when you want to store the context, you can return a function. That function will have the reference to the parent function's variables.
Thx I am accepting your answer..though with the property I am referring to what we have property and method in OOP programming terminology, property like var name = "Mozilla"; where name is the property here..let me know if this is not the case in JavaScript world.
1
return displayName()

this code returning not the function, but result of calling this function.

var myFunc = makeFunc();
myFunc();

the same.in myFunc not the function, but it's result.

try to do this:

return displayName;
...
var myFunc = makeFunc(); //makeFunc() will return displayName() function
myFunc();

Comments

1

Before understanding closures, which is a concept extended from the Javascript function scope. Closures: allows certain variables to get accessed within the scope being created.The inner function can get accessed to the outer function as well as its variables.But the outer function cannot access variables within the inner function.

So in your closure example, the reason why output alerts "Mozilla" is because your inner function-displayName alerts the name variable declared in the outer function (displayName can get access to any variables in the makeFunc function). So when your makeFunc function invokes the inner function-displayName, you will get a TypeError with undefined. Same result will output when you simply call the makeFunc(), you get undefined.

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.