473,414 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Which is considered the best method in this example ?

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+"_"+ar rayidx;
....
}

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
Jul 30 '07 #1
3 1466
d d
d d wrote:
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
Jul 30 '07 #2
On Jul 30, 5:38 am, d d <dd_no_s...@please.netwrote:
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

Jul 30 '07 #3
d d
Peter Michaux wrote:
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
Jul 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Rob | last post by:
i know javascript, vbscript, asp css and alot more and im only 14 i was wondering which is easier to learn php or cgi. any help?
2
by: Rick Trotter | last post by:
I was trying to write some polymorphic application code and found that a superclass method implementation gets invoked when I expect a subclass implementation. Here's how I have abstracted the...
2
by: Elidel | last post by:
Is the use of a COM object in a c# program considered 'unsafe' code ? Does the method that calls the COM object need the 'unsafe' keyword ? Is code that calls a COM object considered unmanaged?...
17
by: lawrence | last post by:
How is it possible that the question "How do I detect which browser the user has" is missing from this FAQ: http://www.faqts.com/knowledge_base/index.phtml/fid/125 and is only here on this...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
4
by: mflll | last post by:
I am looking into the different techniques of handling arrays of edit boxes in Java Script. The first program below works fine. However, are there better ways of doing this, where the person...
15
by: Bob Alston | last post by:
Is it considered best practice to distribute FE databases as MDEs rather than MDBs? Without flaming me for asking the question, could someone please enumerate the key advantages? Also I like to...
1
by: dizzy | last post by:
My client application will have a set of varying business functions. These functions will be fullfiled via various 3rd party services (i.e. web services, http post/get, remote data access or socket...
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.