D
d d
I have a large object with many sub-objects. They may go down 4 or 5
levels and I'm not sure how best to code a method that does operations
on that data. Here's an attempt to make it an isolated example:
var level1={
info:"abc", //lots of other properties here alongside info
level2:{
moreinfo:"def", //lots of properties at this level too
level3:[ //array of level 3 objects
{ path:"imagepath",width:123,height:456,
getImgTag:function(){} //return the tag string on demand
},
{ }] //various other level 3 objects
},
someotherbigobjsatlevel2:{....}
}
This would be called with:
mytag = level1.level2.level3[0].getImgTag();
Inside the getImgTag method, the 'this' property is the level3 object in
the array. It's not the level1 object. This is good from one side, it
means I can access this.path, this.width etc.
On the downside, it means I don't have access to data outside that
object (e.g. the moreinfo string (which has the value "def"), or the
info string even further out (which has the value "abc"). I also don't
know which level3 element I am in the array. This can lead to me needing
to potentially duplicate data.
When I'm generating the img tag, I want to give it an id. I want that id
to be made up of data from level1, level2 and the array index. In this
example, I would want the id to be "abc_def_0". For the image tag for
level3 array element 2, I'd want it to be "abc_def_2".
This means I either need to pass in the info and moreinfo and array
index in the call to getImgTag (this is ugly), or I pass in the whole
object itself and do something like this:
getImgTag:function(mainobj,arrayidx){
var id=mainobj.info+"_"+mainobj.level2.moreinfo+"_"+arrayidx;
....
}
Somehow this is all not coming together as nicely as I'd hoped
Now I can duplicate this info and moreinfo and the array index itself
all into the level3 object but that leads to duplication of data that is
common to all of the level3 objects (and therefore has an argument to be
outside of it). I remember from my OO courses back in the early 90's
that an object should have all the information itself about what it
needs to do, but it's leading to an inefficiency. Would I really want to
do this:
var level1={
info:"abc", //lots of other properties here alongside info
level2:{
moreinfo:"def", //lots of properties at this level too
level3:[ //array of level 3 objects
{ path:"imagepath",width:123,height:456,
iamindex:0,
level1info:"abc",
level2info:"def",
getImgTag:function(){} //return the tag string on demand
},
{ }] //various other level 3 objects
},
someotherbigobjsatlevel2:{....}
}
If the info and moreinfo were only used by these level3 image objects,
then it wouldn't be so bad, but there are so many other object types at
different levels that also need that info. It might end up getting
duplicated dozens of times.
Am I missing the obvious solution? It is monday morning, it's quite
possible.
~dd
levels and I'm not sure how best to code a method that does operations
on that data. Here's an attempt to make it an isolated example:
var level1={
info:"abc", //lots of other properties here alongside info
level2:{
moreinfo:"def", //lots of properties at this level too
level3:[ //array of level 3 objects
{ path:"imagepath",width:123,height:456,
getImgTag:function(){} //return the tag string on demand
},
{ }] //various other level 3 objects
},
someotherbigobjsatlevel2:{....}
}
This would be called with:
mytag = level1.level2.level3[0].getImgTag();
Inside the getImgTag method, the 'this' property is the level3 object in
the array. It's not the level1 object. This is good from one side, it
means I can access this.path, this.width etc.
On the downside, it means I don't have access to data outside that
object (e.g. the moreinfo string (which has the value "def"), or the
info string even further out (which has the value "abc"). I also don't
know which level3 element I am in the array. This can lead to me needing
to potentially duplicate data.
When I'm generating the img tag, I want to give it an id. I want that id
to be made up of data from level1, level2 and the array index. In this
example, I would want the id to be "abc_def_0". For the image tag for
level3 array element 2, I'd want it to be "abc_def_2".
This means I either need to pass in the info and moreinfo and array
index in the call to getImgTag (this is ugly), or I pass in the whole
object itself and do something like this:
getImgTag:function(mainobj,arrayidx){
var id=mainobj.info+"_"+mainobj.level2.moreinfo+"_"+arrayidx;
....
}
Somehow this is all not coming together as nicely as I'd hoped
Now I can duplicate this info and moreinfo and the array index itself
all into the level3 object but that leads to duplication of data that is
common to all of the level3 objects (and therefore has an argument to be
outside of it). I remember from my OO courses back in the early 90's
that an object should have all the information itself about what it
needs to do, but it's leading to an inefficiency. Would I really want to
do this:
var level1={
info:"abc", //lots of other properties here alongside info
level2:{
moreinfo:"def", //lots of properties at this level too
level3:[ //array of level 3 objects
{ path:"imagepath",width:123,height:456,
iamindex:0,
level1info:"abc",
level2info:"def",
getImgTag:function(){} //return the tag string on demand
},
{ }] //various other level 3 objects
},
someotherbigobjsatlevel2:{....}
}
If the info and moreinfo were only used by these level3 image objects,
then it wouldn't be so bad, but there are so many other object types at
different levels that also need that info. It might end up getting
duplicated dozens of times.
Am I missing the obvious solution? It is monday morning, it's quite
possible.
~dd