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
Post a Comment