我有以下代码。我想发送value1的值
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);
如果我在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);
}
答案 0 :(得分: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);
因此,不是在阵列上有一个带有吨的单个对象,而是有一个包含大量对象的单个数组。而这些对象只具有属性。结果更容易概念化和维护,并且整体更清晰。
答案 1 :(得分:0)
如果我理解正确的话,你会对具有相同后缀但前缀不同的属性进行大量的值分配。您可以创建一个函数:
function massAssign(obj, prefix, value) {
obj[prefix + 's'] = value;
obj[prefix + 'sID1'] = value;
obj[prefix + 'sRandom'] = value;
}
然后这样称呼它:
massAssign(n, 'value1', []);