0

While going over John Resig's examples, I wanted to rewrite his assertion examples using Qunit. Everything went well until I got to # 55. In Johns example to pass the test "c" (on line 7) has to be equal "undefined", now according to Qunit in my example "c" has to be equal 8. I surrounded "c" test with console.logs both of which produce undefined. Here's jsbin. Can somebody explain what's going on? me not understand...

Just in case jsbin isn't accessible, here's my code:

var a = 5; 
function runMe(a){
    'use strict';
    test('a', function(){
        strictEqual(a, 6, 'Check the value of a.');
    });

    function innerRun(){
        test('b', function(){
            strictEqual(b, 7, 'Check the value of b.');
        });
        console.log(c);
        test('c', function(){
            //the problem seems to be here
            strictEqual(c, 8, 'Check the value of c.');
        });
        console.log(c);
    }

    var b = 7;
    innerRun();
    var c = 8;
} 
runMe(6); 

for ( var d = 0; d < 3; d++ ) {
    setTimeout(function(){ 
        test('d', function(){
            strictEqual(d, 3, 'Check the value of d.');
        });
    }, 100); 
}

However, if I put console.log from within the "c" test it logges out 8.

Mr Wilde
  • 649
  • 8
  • 19
user
  • 2,939
  • 5
  • 26
  • 39
  • Clearly, when you call innerRun() c hasn't been set yet, so it's undefined during the 'assertion c' test. Are you saying the check against 8 succeeds? – 500 - Internal Server Error Feb 05 '13 at 02:08
  • Here http://jsbin.com/esipaf/10 check it. It seems to succeed. – user Feb 05 '13 at 02:20
  • No it is defined. All `var` statements are moved together at the beginning of the block. To make this obvious, some code styles require you to write one `var` statement at the beginning of a block. See also http://stackoverflow.com/a/1236270/549755 – Odi Feb 05 '13 at 15:08
  • But how do you explain John Resig's example? Why in his code c is "undefined"? That's what i want to know. Hoisting was the first thing that came to my mind, but I could not explain John's results. – user Feb 05 '13 at 15:34
  • Hoisting explains why `c` is defined. The reason why it actually has the value `8` is, that you define a test function for QUnit, which is executed later. Ib this small timeframe the assigment `var c = 8` takes place, thus explaining the result. But John Resig is using assert(), which is executed immediatly. – Odi Feb 06 '13 at 07:26
  • @Odi so can you put it in an answer, your previous comment, so I can mark it? – user Feb 06 '13 at 14:59

1 Answers1

1

All var statements are moved together at the beginning of the block (hoisting). Hoisting explains why c is defined. The reason why it actually has the value 8 is, that you define a test function for QUnit, which is executed later.

In the small time frame between the definition of the function and the execution the assignment var c = 8 takes place, thus explaining the result. But John Resig is using assert(), which is executed immediately.

Odi
  • 6,916
  • 3
  • 34
  • 52