0

I'm looking for a method to update arrays via closure. Is there anything that would be more efficient than this.

p.s. I'm not actually using inputs in this manner to set values. They are being passed via another function. Just needed an easy way to get my main point across.

  <script>
    sampleArrayValues=['copy','this','array'];
a="none";   

function updateArr(val,index,action) {
    var v=value;
    var i=index;
    var a=action;
    var arr=[];

    return function(v,i,a) {
        if(a=="none"){
        }
        if(a=="copy"){
            arr=sampleArrayValues;  
        }
        if(a=="update"){
            arr[index]=val; 
        }
        return arr;
    }
} 


updateArr.update1 = updateArr(0,0,a);

function scoopValues(){
    var val=0 || document.getElementById('one').value;
    var index=0 || document.getElementById('two').value;
    var action="none" || document.getElementById('three').value;
    alert(updateArr.update1(val,index,action))
}

   Enter value: <input id="one" style="width:20px;height:20px"/>
    <br>
      Enetr index: <input id="two"style="width:20px;height:20px"/>
     <br>
  Enter Action  "none" "copy" or "update": <input id="three"style="width:60px;height:20px"/>
  <br>
   <input type="button" onclick="scoopValues()" value="click to update"style="width:100px;height:25px"/>
user1740058
  • 45
  • 1
  • 9
  • I will ponder over your question, but may be it it better to post it at [codereview.stackexchange.com](http://codereview.stackexchange.com) – winner_joiner Mar 07 '13 at 08:20
  • sorry I was missing the bottom of the code with the inputs – user1740058 Mar 07 '13 at 08:21
  • What should `copy` do? Now it does the same as `none`. Btw, your question is not really fit for StackOverflow, CodeReview would be the right place to ask it, as already advised to you. – kapa Mar 07 '13 at 08:24
  • `testArr=testArr;` in the return value of the **updateArr** seems a bit of, or am i missing something? – winner_joiner Mar 07 '13 at 08:25
  • You don't need statements like `var value=value;`. Function arguments are local variables. – Barmar Mar 07 '13 at 08:31
  • The closure arguments have the same names as the variables being closed over, so these arguments are shadowing them. The only closure variable you're actually using is `arr`. Before you worry about efficiency, how about getting the code right? – Barmar Mar 07 '13 at 08:35

0 Answers0