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)"?

gdoron
  • 147,333
  • 58
  • 291
  • 367
user1386284
  • 95
  • 1
  • 7
  • 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. – zzzzBov May 29 '12 at 22:08

6 Answers6

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.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • No, it doesn't work even though I tried it at the way your suggested. The output is "undefined". Please see the following link: https://developer.mozilla.org/en/JavaScript/Guide/Functions#Calling_functions – user1386284 May 29 '12 at 23:24
  • 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 May 30 '12 at 05:42
  • @user1386284 exactly - that "reading first" is what's normally known as "hoisting" – Alnitak May 30 '12 at 06:51
1

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

  
 }

 s();
 
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));  
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • @Alnitak. Why do you think that code snippet is in the global object? can't it be in an inner scope!? – gdoron May 29 '12 at 22:18
  • 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 May 29 '12 at 22:20
  • @Alnitak. It's still true, it's internal to the function scope!, and the scope in the global object is... the global object. – gdoron May 29 '12 at 22:22
  • No, it doesn't work even though I tried it at the way your suggested. The output is "undefined". Please see the following link: https://developer.mozilla.org/en/JavaScript/Guide/Functions#Calling_functions – user1386284 May 29 '12 at 23:23
  • 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 May 30 '12 at 06:41
  • @gdoron, by the way, can you please teach me how to use the jsfiddle.net that you posted from the link of Live DEMO. I saw the js code there but no html code. How do you see the result? What html code do you suggest if that is needed? Thank you. – user1386284 May 30 '12 at 06:47
  • @user1386284. Read the [docs of the site](http://doc.jsfiddle.net/). `console.log(...)` uses the developers console for the output, you have to use a suitable browser for it. – gdoron May 30 '12 at 07:25
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)

Esailija
  • 138,174
  • 23
  • 272
  • 326
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?

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
-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();

 
  • Please add more information and/or description of your answer to make others easily understand it. – koceeng Jan 08 '17 at 06:05