javascript - Access Variable from a different function without creating global variables -


i've started using casperjs web automation, , confusing me little.

how access local variables 1 function another? example:

casper.start('http://google.com', function(){     var somevar = 20; });  casper.thenopen('http://google.com/analytics', function(){     // how can access somevar?jav }); 

i know somevar not in scope in second function, how access somevar in second function without defining globals?

without using globals say, create third function (bean) have var somevar = 20; local variable , provide getter , setter functions use others.

var sharedspace = (function(){   var shared = 20; //initialization   return {     getshared: function(){                        return shared;                     },     setshared: function(val){                        shared = val;                     }     }  })();    (function(){      alert(sharedspace.getshared());      sharedspace.setshared(500)  })();    (function(){      alert(sharedspace.getshared());      sharedspace.setshared(400)  })();    (function(){      alert(sharedspace.getshared());      sharedspace.setshared(10)  })();


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -