Problem in homemade function to merge objects
- by Eric
I'm trying to make a function that merges arrays. The reason is, I have a function that supposed to get the settings of an entity, and merge them with the global defaults.
//So for example, let's say globalOptions is something like this
var globalOptions={opt1:'foo',opt2:'something'};
//and this is entityOptions
var entityOptions={opt1:'foofoo',opt2:null};
The only difference is it has objects in objects and objects in objects in objects, so what I made was a function that loops through all objects, thinking I would later, easily be able to loop through it all. Please ignore the array thing. That is defected, but unneeded.
function loopObj(obj, call, where, objcall, array) {
if ($.isArray(obj) || $.isPlainObject(obj)) {
for (i in obj) {
if ($.isArray(obj)) {
if (array) {
loopObj(obj[i], call, where[where.length] = i, true);
if (objcall) {
call(obj[i],where,true);
}
}
else {
loopObj(obj[i], call, where+'['+i+']', false);
if (objcall) {
call(obj[i],where,true);
}
}
}
else {
if (array) {
loopObj(obj[i], call, where[where.length] = parseInt(i), true);
if (objcall) {
call(obj[i],where,true);
}
}
else {
loopObj(obj[i], call, where+'[\''+i+'\']', false);
if (objcall) {
call(obj[i],where,true);
}
}
}
}
}
else {
return call(obj,where);
}
}
Then I made this program to convert it:
function mergeObj(a,b) {
temp.retd = new Object();
loopObj(a,function (c,d) {
if (c) {
eval(d.replace('%par%','temp.retd'))=c;
}
else {
eval(d.replace('%par%','temp.retd'))=eval(d.replace('%par%','b'));
}
},'%par%', true);
return temp.retd();
}
I get the error:
Uncaught ReferenceError: Invalid left-hand side in assignment
(anonymous function)base.js:51
loopObjbase.js:40
loopObjbase.js:31
mergeObjbase.js:46
(anonymous function)base.js:72
I know what it means, the eval returns an anonomys variable (copy of the variable), so I can't set it, only get it.