0

I was reading the book javascript : The Good Parts, under The function Statement Versus the function Expression, section B.9. There is a line which says that

function statements are subject to hoisting. This means that regardless of where a function is placed, it is moved to the top of the scope in which it is defined. This relaxes the requirement that functions should be declared before used, which I think leads to sloppiness

so according to this i did a simple experiment as follows:

<html>
<script type="text/javascript">


var x = function(){

    alert(s);

    var s = function(){
        return 'sanmevg'; 
    }

};  
</script>   

<body>


<button onclick="x();">click here</button>  

</body></html>

Now according to this book it clearly states that:

it is moved to the top of the scope in which it is defined. This relaxes the requirement that functions should be declared before used

so this means that when that button is clicked then there should be an alert saying sanmveg. but the result was unexpected, it return Undefined.

My Questions

1) Should i take those statement written by Douglas Crockford ,correct for some special case(if yes then it would be so kind enough to tell me those cases)?

2) Is it reflecting some other meaning?

3) Do i have done something wrong in my experiment?


Thank you for all your answers!

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
anni
  • 290
  • 1
  • 3
  • 15

2 Answers2

0

Because that is not a function declaration :

var s = function(){
    return 'sanmevg'; 
}

it is an anonymous function assigned to a variable. Change it to that and it will work :

function s(){
    return 'sanmevg'; 
}

As stated by Phylax :

"Note that var s will be hoisted. That is why the alert() show undefined instead of an error. But the assigned will stay where it is."

Community
  • 1
  • 1
Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
  • 3
    Note that `var s` will be hoisted. That is why the `alert()` show undefined instead of an error. But the assigned will stay where it is. – phylax Apr 11 '14 at 15:27
  • @phylax I think that's a important note, may I include it in the answer? – Karl-André Gagnon Apr 11 '14 at 15:29
  • okey thanks... and by the way from your line `it is an anonymous function assigned to a variable` can i write `var s = function s(){..}` because a non-anonymous function is assined to a variable – anni Apr 11 '14 at 15:32
  • @anni yes you can! http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname/338053#338053 – Karl-André Gagnon Apr 11 '14 at 15:34
0

What you typed is a function expression and not a function declaration.

A function declaration gets hoisted, which is why you can call the function above where you define it and what you were reading about.

function myFunction(){
  alert('myFunction');
}

However, a function expression is what you typed. The declared variable gets hoisted, yet the function assignment remains in place.

var myFunction = function(){
  alert('myFunction');
}
nateyolles
  • 1,851
  • 5
  • 17
  • 23