Jquery 无法将对象作为参数传递
我正在疯狂地试图理解为什么我不能成功地将一个对象作为论点传递。当我这么做的时候,奇怪的事情正在发生。当我将对象作为文本传递时,它可以正常工作 我试图通过提供一个布局和一个数据集并将它们合并到一个对象中来构建一个表。它正在工作,只是将所有行的数据设置为相同。我发现只有当我将模板作为参数传递时才会发生这种情况 我想要的结果Jquery 无法将对象作为参数传递,jquery,arrays,Jquery,Arrays,我正在疯狂地试图理解为什么我不能成功地将一个对象作为论点传递。当我这么做的时候,奇怪的事情正在发生。当我将对象作为文本传递时,它可以正常工作 我试图通过提供一个布局和一个数据集并将它们合并到一个对象中来构建一个表。它正在工作,只是将所有行的数据设置为相同。我发现只有当我将模板作为参数传递时才会发生这种情况 我想要的结果 First Row - First Col First Row - Sec Col Sec Row - First Col Sec Row - Sec Col Sec Row
First Row - First Col
First Row - Sec Col
Sec Row - First Col
Sec Row - Sec Col
Sec Row - First Col
Sec Row - Sec Col
Sec Row - First Col
Sec Row - Sec Col
我的实际成绩
First Row - First Col
First Row - Sec Col
Sec Row - First Col
Sec Row - Sec Col
Sec Row - First Col
Sec Row - Sec Col
Sec Row - First Col
Sec Row - Sec Col
什么不起作用:
var data= {
0 : {
col1 : 'First Row - First Col' ,
col2 : 'First Row - Sec Col'
},
1 : {
col1 : 'Sec Row - First Col',
col2 : 'Sec Row - Sec Col'
}
}
var template= {
col1 : {
attr : {id : '44'},
css : {width : '150px'},
html : ''
},
col2 : {
attr : {id : 1},
css : {width : '150px'},
html : ''
}
}
/**
* Merge the template and the data
* @param {Object} template - The table's template
* @param {Object} data - The table's data
* @returns {Object} tableData - The merged template and data
*/
Table.prototype.dataToTable = function(template, data){
var tableData = {};
var rowCount = Object.keys(data).length;
for(i = 0; i < rowCount; i++){
// Here is the issue. If I use = template
// then both rows are somehow the same. But
// if I inject my layout object directly, it's fine!
tableData[i] = template;
$.each(tableData[i], function(k,v){
v.html = data[i][k];
});
}
return tableData;
}
var数据={
0 : {
col1:'第一行-第一列',
col2:“第一行-第二列”
},
1 : {
col1:'秒行-第一列',
col2:'秒行-秒列'
}
}
变量模板={
第1栏:{
属性:{id:'44'},
css:{width:'150px'},
html:'
},
第2栏:{
属性:{id:1},
css:{width:'150px'},
html:'
}
}
/**
*合并模板和数据
*@param{Object}template-表的模板
*@param{Object}data-表的数据
*@returns{Object}tableData-合并的模板和数据
*/
Table.prototype.dataToTable=函数(模板、数据){
var tableData={};
var rowCount=Object.keys(data).length;
对于(i=0;i
什么有效
Table.prototype.dataToTable = function(template, data){
var tableData = {};
var rowCount = Object.keys(data).length;
for(i = 0; i < rowCount; i++){
tableData[i] = {
col1 : {
attr : {id : '44'},
css : {width : '150px'},
html : ''
},
col2 : {
attr : {id : 1},
css : {width : '150px'},
html : ''
}
};
$.each(tableData[i], function(k,v){
v.html = data[i][k];
});
}
return tableData;
}
Table.prototype.dataToTable=函数(模板,数据){
var tableData={};
var rowCount=Object.keys(data).length;
对于(i=0;i
问题在于Javascript中的对象仅通过引用进行传输,因此当您执行oneObject=secondObject时,oneObject和secondObject引用将是相同的。当你改变一个,你就改变了另一个。 您需要的是克隆模板对象
var clone = function () {
var cloneFn = function () {
var newObj = (this instanceof Array ? [] : {});
for (var index in this) {
if (this.hasOwnProperty(index)) {
if (index == 'clone')
continue;
if (this[index] && typeof this[index] == 'object')
newObj[index] = cloneFn.apply(this[index]);
else {
if (typeof (this[index]) == 'string')
newObj[index] = this[index];
else
newObj[index] = this[index];
}
}
}
return newObj;
};
return cloneFn.apply(this);
};
tableData[i] = clone.apply(template);
或者类似的东西
玩得开心,许多微笑对象通过引用传递。在
$中。每个
循环实际上都在更改模板
中的值。请参阅这篇关于对象克隆的帖子~。您可能可以使用tableData[i]=$.extend(true,{},template)
谢谢,您应该作为答案发布