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

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

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

html - jQuery UI Sortable - Remove placeholder after item is dropped -