2010-05-27 57 views
0

我有下面的代碼。我想值1的值發送發送不同的對象值到不同的功能

n.value1s = new Array(); 
n.value1sIDs = new Array(); 
n.value1sNames = new Array(); 
n.value1sColors = new Array(); 
n.descriptions = new Array(); 

pg.loadLinkedvalue1s(n); 

和VALUE2至pg.loadLinkedvalue2s(N);
Howd我這樣做,在javascript巡航能力不重寫功能齊全
請查看下面

if(n.id == "row"){ 
     n.rs = n.parentElement; 
     if(n.rs.multiSelect == 0){ 
      n.selected = 1; 
      this.selectedRows = [ n ]; 
      if(this.lastClicked && this.lastClicked != n){ 
       selectionChanged = 1; 
       this.lastClicked.selected = 0; 
       this.lastClicked.style.color = "000099"; 
       this.lastClicked.style.backgroundColor = ""; 
      } 
     } else { 
      n.selected = n.selected ? 0 : 1; 
      this.getSelectedRows(); 
     } 
     this.lastClicked = n; 


      n.value1s = new Array(); 
      n.value1sIDs = new Array(); 
      n.value1sNames = new Array(); 
      n.value1sColors = new Array(); 
      n.descriptions = new Array(); 
      n.value2s = new Array(); 
      n.value2IDs = new Array(); 
      n.value2Names = new Array(); 
      n.value2Colors = new Array(); 
      n.value2SortOrders = new Array(); 
      n.value2Descriptions = new Array();  
    var value1s = myOfficeFunction.DOMArray(n.all.value1s.all.value1);  
    var value2s = myOfficeFunction.DOMArray(n.all.value1s.all.value2); 

      for(var i=0,j=0,k=1;i<vaue1s.length;i++){ 

       n.sortOrders[j] = k++; 
       n.vaue1s[j] = vaue1s[i].v; 
       n.vaue1IDs[j] = vaue1s[i].i; 
       n.vaue1Colors[j] = vaue1s[i].c; 
       alert(n.vaue1Colors[j]); 

    var vals = vaue1s[i].innerText.split(String.fromCharCode(127));  

       n.cptSortOrders[j] = k++; 
       n.value2s[j] = value2s[i].v; 
       n.value2IDs[j] = value2s[i].i; 
       n.value2Colors[j] = value2s[i].c;  
var value2Vals = value2s[i].innerText.split(String.fromCharCode(127));  

       if(vals.length == 2){ 
        alert(n.vaue1Colors[j]);  
        n.vaue1Names[j] = vals[0]; 
        n.descriptions[j++] = vals[1]; 

       } 

       if(value2Vals.length == 2){ 

      n.value2Names[j] = cptVals[0];  
        alert(n.value2Names[j]);  
     n.cptDescriptions[j++] = cptVals[1];  
      alert(n.cptDescriptions[j++]);  
       }     

      } 


     //want to run this with value1 only 
       pg.loadLinkedvalue1s(n); 

       // want to run this with value2 only 
       pg.loadLinkedvalue2s(n);  


    } 
+1

你不必寫'n.value1s =新的Array();'(等人),' n.value1s = [];'是方法更短。 – 2010-05-27 21:27:50

回答

2
function makeValueObject() { 
    return { 
    values: [], 
    ids: [], 
    names: [], 
    colors: [], 
    // etc... 
    } 
} 

n.value1 = makeValueObject(); 
n.value2 = makeValueObject(); 

n.value1.values.push('123'); 

function loadLinkedValues(valueObject) { 
    // do stuff with valueObject // 
} 

loadLinkedValues(n.value1); 

代碼另外,我假設,數組一起去。意思是values[i]colors[i]有關。如果是這種情況,你可以讓這個更容易管理。

function makeThing() { 
    return { 
    value: abc, 
    id: 345, 
    name: 'Cool Spiffy Thing', 
    color: 'red' 
    } 
} 

n.group1 = []; 
n.group1.push(makeThing()); 
n.group1[0].name = 'Changed named here'; 

n.group1.push(makeThing()); 
n.group1.push(makeThing()); 

n.group2 = [makeThing()]; 

function loadLinkedValues(valueObjects) { 
    for (var i=0; i < valueObjects.length; i++) { 
    // do stuff with each object // 
    console.log(valueObjects[i].name +'has a value of: '+ valueObjects[i].value); 
    } 
} 

loadLinkedValues(n.group1); 

所以不是在數組上有一個單獨的對象,而是有一個包含大量對象的單個數組。而那些對象只是有屬性。結果更容易概念化和維護,並且整體更清潔。

+0

+1比原來更具可讀性。 – wombleton 2010-05-27 22:43:11

0

如果我理解正確,您正在爲具有相同後綴但不同前綴的屬性進行質量分配。你可以創建一個函數:

function massAssign(obj, prefix, value) { 
    obj[prefix + 's'] = value; 
    obj[prefix + 'sID1'] = value; 
    obj[prefix + 'sRandom'] = value; 
} 

再這樣稱呼它:

massAssign(n, 'value1', []);