8

It makes sense by calling the function this way:

print(square(5));  
function square(n){return n*n}

But why the following calling doesn't work?

print(square(5));  
square = function (n) {  
  return n * n;  
}  

What's the solution if we insist to use the format of "square = function (n)"?

1
  • 1
    If you think about what would happen in the following code: foo = function () { alert('bar') }; foo(); foo = function () { alert('baz'}; the reasoning should become apparent. Commented May 29, 2012 at 22:08

6 Answers 6

12

"normal" function declarations are hoisted to the top of the scope, so they're always available.

Variable declarations are also hoisted, but the assignment doesn't happen until that particular line of code is executed.

So if you do var foo = function() { ... } you're creating the variable foo in the scope and it's initially undefined, and only later does that variable get assigned the anonymous function reference.

If "later" is after you tried to use it, the interpreter won't complain about an unknown variable (it does already exist, after all), but it will complain about you trying to call an undefined function reference.

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

3 Comments

No, it doesn't work even though I tried it at the way your suggested. The output is "undefined". Please see the following link: developer.mozilla.org/en/JavaScript/Guide/…
You are right. It complaint the print function. I also realized the reason why value function doesn't work as function expression is because the function expression is read first before all other codes; while the value function is read as normal flow.
@user1386284 exactly - that "reading first" is what's normally known as "hoisting"
1

    var s=function ()
	{
		console.log("hi there");
        document.write("function express called");
		alert("function express called");

		
	}

	s();
 

Comments

0

You need to change the order, you use the variable before it was declared and assigned:

square = function (n) {//Better use "var" here to avoid polluting the outer scope
  return n * n;  
}  
print(square(5));  

Correct way with var :

var square = function (n) { // The variable is now internal to the function scope
  return n * n;  
}  
print(square(5));  

7 Comments

@Alnitak. Why do you think that code snippet is in the global object? can't it be in an inner scope!?
sure, it can be, but I think it's unsafe to assume that the OP is putting this code inside another function, per your comment in the second code example.
@Alnitak. It's still true, it's internal to the function scope!, and the scope in the global object is... the global object.
No, it doesn't work even though I tried it at the way your suggested. The output is "undefined". Please see the following link: developer.mozilla.org/en/JavaScript/Guide/…
You are right. It complaint the print function. I also realized the reason why value function doesn't work as function expression is because the function expression is read first before all other codes; while the value function is read as normal flow.
|
0

In function expression you are using the function like any other value, would you expect:

print(a);
var a = 5

to work? (I'm not really asking)

Comments

0

In the second case, square is a regular variable subject to (re)assignment. Consider:

square = function (n) {  
  return "sponge";  
}  
print(square(5));  
square = function (n) {  
  return n * n;  
}  

What would you expect the output to be here?

Comments

-1

var s=function ()
	{

		console.log("s");
		alert("function expression with anomious function");

		
	}

s();

var otherMethod=function ()
	{

		console.log("s");
		alert("function expression with function name");

		
	}

otherMethod();

	

1 Comment

Please add more information and/or description of your answer to make others easily understand it.

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.