D
dd
I'm writing something in JS using the latest OO and JSON
and I'm looking for a bit of guidance. I'm going to have this
large object which has many top-level properties and some
top-level functions. That part I have no problem with, I'll
define it in JSON style (my prototype is working just fine).
What I'll also have though, is some arrays at the top level.
These arrays will contain n number of sub-objects. Each
of these sub-objects will also have properties and arrays
of their own. Again, no problem so far.
What I would like advice on is the strategy for adding
functions to these sub-objects in the most efficient way.
Let's say I have an array of 10 sub-objects, it would be
highly inefficient to define the function inline in each of the
10 sub-object definitions. I'm thinking it's probably better
to declare these things in a loop after the end of the
main object definition, something like this:
for(var i=0;i<mainobj.subobjarray.length;i++)
mainobj.subobjarray.somefunc=function(){bla();}
Obviously that func definition wouldn't be a one-liner.
Also, does anyone have a good suggestion for how best
to align JSON structure definitions? Here's the kind of
thing I have so far:
mainobj = {
id:123,
name:"fred",
type:"whatever",
toplevelfunc:function(){do_stuff();},
toplevel2:function(){do_more_stuff();},
subobjarray:
[ { num:0,
name:"hello",
foo:"bar",
func_here:function(){is_a_bad_idea();}
otherarray:
[ { bla:"bla",
like:"whateva!"
},
{ bla:"what?",
like:"hi"
}
]
},
{ num:1,
name:"hello",
foo:"bar",
func_here:function(){is_a_bad_idea();}
otherarray:
[ { bla:"bla",
like:"whateva!"
},
{ bla:"what?",
like:"hi"
}
]
}
],
final_property:"the_end",
final_func:function(){alert("really the end");
};
//add functions into the arrays of sub-obj's here.
and I'm looking for a bit of guidance. I'm going to have this
large object which has many top-level properties and some
top-level functions. That part I have no problem with, I'll
define it in JSON style (my prototype is working just fine).
What I'll also have though, is some arrays at the top level.
These arrays will contain n number of sub-objects. Each
of these sub-objects will also have properties and arrays
of their own. Again, no problem so far.
What I would like advice on is the strategy for adding
functions to these sub-objects in the most efficient way.
Let's say I have an array of 10 sub-objects, it would be
highly inefficient to define the function inline in each of the
10 sub-object definitions. I'm thinking it's probably better
to declare these things in a loop after the end of the
main object definition, something like this:
for(var i=0;i<mainobj.subobjarray.length;i++)
mainobj.subobjarray.somefunc=function(){bla();}
Obviously that func definition wouldn't be a one-liner.
Also, does anyone have a good suggestion for how best
to align JSON structure definitions? Here's the kind of
thing I have so far:
mainobj = {
id:123,
name:"fred",
type:"whatever",
toplevelfunc:function(){do_stuff();},
toplevel2:function(){do_more_stuff();},
subobjarray:
[ { num:0,
name:"hello",
foo:"bar",
func_here:function(){is_a_bad_idea();}
otherarray:
[ { bla:"bla",
like:"whateva!"
},
{ bla:"what?",
like:"hi"
}
]
},
{ num:1,
name:"hello",
foo:"bar",
func_here:function(){is_a_bad_idea();}
otherarray:
[ { bla:"bla",
like:"whateva!"
},
{ bla:"what?",
like:"hi"
}
]
}
],
final_property:"the_end",
final_func:function(){alert("really the end");
};
//add functions into the arrays of sub-obj's here.