javascript - Accessing a "parent" functions' variable -


in javascript there way have mod() modify a? want do:

function whatever(){   var a=0;   mod(a);   var amodded=a; } function mod(obj){   obj++   a=obj; } 

at end of whatever() want 'amodded' =1. there way without declaring 'a' outside of , functions?

1. make mod such changes [properties of] a directly:

function whatever (){     var = {value: 0};     mod(a);     var amodded = a.value; }  function mod(obj){     obj.value++; } 

http://jsfiddle.net/derekl/2jl0we41/

2. make mod such returns new value:

function whatever (){     var       = 0,         amodded = mod(a); }  function mod(obj){     return ++obj; } 

http://jsfiddle.net/derekl/jv9ya841/


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -