Variable scope in node.js or casper.js
Problem
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('http://google.com/', function() {
this.echo("I'm in");
});
casper.run(function(aaa) {
this.echo(aaa);
this.exit();
});
Problem courtesy of: HP.
Solution
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
Discussion
There is currently no discussion for this recipe.
This recipe can be found in it's original form on Stack Over Flow.