For example we have an object like the following
Copy Code code as follows:
var obj = {a:{
B: "BB"
}
}
But now we want to add the following attribute to the Obj object obj.a.b.c.d.f= "FF"; We will generally do the following, obj.a.b.c={},obj.a.b.c.d={}, obj.a.b.c.d.f= "FF", but if I have a lot of properties, such a method is not feasible. Now provides a way to automatically generate object properties
Copy Code code as follows:
function Autocreateobjproperty (temstring) {
var temobjs = Temstring.split (".");
for (var i =0;i<temobjs.length;i++) {
var ttt = temobjs[i];
if (!obj.hasownproperty (Temobjs[i])) {
var objstring= "obj";
for (var j= 1;j<=i;j++) {
objstring+= "." +TEMOBJS[J];
}
obj = eval (objstring);
if (obj = = undefined) {
var temobjstring= "obj"; The name of the object var obj = {}
for (var j= 1;j<i;j++) {
temobjstring+= "." +TEMOBJS[J];
}
obj = eval (temobjstring);
obj[temobjs[i]]={};
obj={};
}
}else{
obj = Obj[temobjs[i]];
}
}
return obj;
}