Variable scope in node.js or casper.js


I feel stupid asking this question. But I have an annoying issue with variable scoping aaa below. Shouldn't it output test at the end? But that wasn't the case. What did I do wrong to use aaa within a function?

var casper = require('casper').create();

var aaa = 'test';
casper.start('', function() {
    this.echo("I'm in");
}); {
Problem courtesy of: HP.


When you echo out aaa, it uses the aaa as defined in the first parameter for your function. If your first parameter was called bbb or something, then you would see the output that you expect, as it would use the aaa defined in the outer closure.

Solution courtesy of: Brad


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.