Which is considered the best method in this example ?

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
 
D

d d

d said:
Am I missing the obvious solution? It is monday morning, it's quite
possible.

Well, it seems like I was missing the obvious. I moved my method to the
root of the object, and pass in only one param - the array index.

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 },
{ }] //various other level 3 objects
},
getImgTag:function(idx){
//I have access to info, moreinfo, and the array
//and can create a ptr to the obj I want to work on
//from the idx param:
var obj=this.level2.level3[idx];
//use obj.width, obj.height
} //return the tag string on demand
}

If there's a better way, I'd still like to hear about it...

~dd
 
P

Peter Michaux

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".

Can you build this big object in phases? Your objects are never sealed
in JavaScript so you can always modify them in stages.

var level1 = {
info:"abc"
};

level1.level2 = {
blah: "dfe"+level1.info
zip: function() {return this.blah + level1.info;}
};

Peter
 
D

d d

Peter said:
Can you build this big object in phases? Your objects are never sealed
in JavaScript so you can always modify them in stages.

var level1 = {
info:"abc"
};

level1.level2 = {
blah: "dfe"+level1.info
zip: function() {return this.blah + level1.info;}
};
Peter

I will be building it in phases. The initial object will contain "the
data" and one line of JS which loads a standard JS file. That standard
JS file will thten be adding some extra default/constant data and the
methods to the object.

I see what you're saying, the data can be easily duplicated while
building the object. It's still duplication though. I think I'd prefer
the methods being at the root (having access to the whole object via
this), and they receive a ptr to the sub-object to work on, or an index
into an array of sub-objects.

~dd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top