467,895 Members | 1,414 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,895 developers. It's quick & easy.

Is it possible to include and object inside of any object using JSON.

Daz
Hi everyone. Sorry for the confusing subject, I couldn't think how best
to word it.

What I would like to know, is if there is an equivilant to this code,
in using JSON.

<script type="text/javascript">
function makeTable(number_of_rows) {
var table = document.createElement('table');
table.border = 1;

number_of_rows = (number_of_rows < 0) ? 0 :
number_of_rows;
number_of_rows = (number_of_rows 100) ? 100 :
number_of_rows;

for (var i = 0; i < number_of_rows; i++)
{
table.appendChild(new tableRow);
}
return table;

function tableRow()
{
var tr = document.createElement('tr');
var td1 = document.createElement('td');
var td2 = document.createElement('td');
tr.appendChild(td1);
tr.appendChild(td2);
td1.textContent = 'Some Data';
td2.textContent = 'Some More Data';
return tr;
}
}

var newTable = new makeTable(5);
document.body.appendChild(newTable);
</script>

Note that the makeTable constructer is calling another constructer
(tableRow) several times, and making a totally new row each time. Would
it also be possible for me to call upon the constructer for tableRow
from outside of the makeTable object? For example:

var newRow = new makeTable.tableRow();

How would I be able to do this? I have read endless tutorials, but I
can't seem to find the answer. Perhaps the answer is obvious, and I
just haven't realised?

All the best.

Daz.

Dec 21 '06 #1
  • viewed: 1279
Share:
2 Replies
Daz wrote:
Hi everyone. Sorry for the confusing subject, I couldn't think how best
to word it.

What I would like to know, is if there is an equivilant to this code,
in using JSON.
Remove the script tags and eval it. Note that declared functions and
variables will only be available within the eval scope and will cease
to be available after eval has finished.
>
<script type="text/javascript">
function makeTable(number_of_rows) {
var table = document.createElement('table');
table.border = 1;

number_of_rows = (number_of_rows < 0) ? 0 :
number_of_rows;
Indent posted code using 2 or 4 spaces, manually wrap code at about 70
characters to prevent auto-wrapping.
number_of_rows = (number_of_rows 100) ? 100 :
number_of_rows;

for (var i = 0; i < number_of_rows; i++)
{
table.appendChild(new tableRow);
It would make more sense at this point to do something like:

var row = table.insertRow(-1);
for (var j=0; j<number_of_cells; j++){
var cell = row.insertCell(j); // or row.insertCell(-1)
/* add content to cell */
}

It doesn't make sense to create a new function, however if this is just
for learning then proceed.

[...]
</script>

Note that the makeTable constructer is calling another constructer
They are not "constructors" in the classic OO sense, they are plain
function objects.
(tableRow) several times, and making a totally new row each time. Would
it also be possible for me to call upon the constructer for tableRow
from outside of the makeTable object? For example:

var newRow = new makeTable.tableRow();
Not the way you have written it. You need to add tableRow as a
property of makeTable in the scope that you wish to call it. Declaring
it inside makeTable makes it available only from within makeTable
unless you do something to make it available elsewhere.

Consider:

function foo(){
function bar(){
alert('bar');
}
window.bar = bar;
}

alert('bar: ' + typeof bar);

foo();

alert('bar: ' + typeof bar);
The function object bar is created inside foo as soon as the code is
parsed by the javascript engine. However, it is not assigned to the
value of window.bar until the function foo is executed so the first
alert shows "bar: undefined" and the second "bar: function".

How would I be able to do this? I have read endless tutorials, but I
can't seem to find the answer. Perhaps the answer is obvious, and I
just haven't realised?
There are a number of methods, which is "best" depends on the
situation. If this is for a utility function, the simplest way is to
create a namespace object then add everything as a property of that
object:

var tableUtilities = {};

tableUtilities.makeTable = function(param1, param2)
{
/* function body */
alert('makeTable: ' + param1 + ' ' + param2);
}

tableUtilities.makeTable.makeRow = function(param1, param2)
{
/* function body */
alert('makeTable.makeRow: ' + param1 + ' ' + param2);
}

tableUtilities.makeTable('firstParam', 'secondParam');
tableUtilities.makeTable.makeRow('firstParam', 'secondParam');
Of course in this context, it doesn't make much sense for makeRow to be
a property of makeTable. You may like to read the following articles:

Douglas Crockford - Private Members in JavaScript
<URL: http://www.crockford.com/javascript/private.html >

Richard Cornford - Private Static Members in Javascript
<URL: http://www.litotes.demon.co.uk/js_in...te_static.html >
--
Rob

Dec 22 '06 #2
Daz

RobG wrote:
Daz wrote:
Hi everyone. Sorry for the confusing subject, I couldn't think how best
to word it.

What I would like to know, is if there is an equivilant to this code,
in using JSON.

Remove the script tags and eval it. Note that declared functions and
variables will only be available within the eval scope and will cease
to be available after eval has finished.

<script type="text/javascript">
function makeTable(number_of_rows) {
var table = document.createElement('table');
table.border = 1;

number_of_rows = (number_of_rows < 0) ? 0 :
number_of_rows;

Indent posted code using 2 or 4 spaces, manually wrap code at about 70
characters to prevent auto-wrapping.
number_of_rows = (number_of_rows 100) ? 100 :
number_of_rows;

for (var i = 0; i < number_of_rows; i++)
{
table.appendChild(new tableRow);

It would make more sense at this point to do something like:

var row = table.insertRow(-1);
for (var j=0; j<number_of_cells; j++){
var cell = row.insertCell(j); // or row.insertCell(-1)
/* add content to cell */
}

It doesn't make sense to create a new function, however if this is just
for learning then proceed.

[...]
</script>

Note that the makeTable constructer is calling another constructer

They are not "constructors" in the classic OO sense, they are plain
function objects.
(tableRow) several times, and making a totally new row each time. Would
it also be possible for me to call upon the constructer for tableRow
from outside of the makeTable object? For example:

var newRow = new makeTable.tableRow();

Not the way you have written it. You need to add tableRow as a
property of makeTable in the scope that you wish to call it. Declaring
it inside makeTable makes it available only from within makeTable
unless you do something to make it available elsewhere.

Consider:

function foo(){
function bar(){
alert('bar');
}
window.bar = bar;
}

alert('bar: ' + typeof bar);

foo();

alert('bar: ' + typeof bar);
The function object bar is created inside foo as soon as the code is
parsed by the javascript engine. However, it is not assigned to the
value of window.bar until the function foo is executed so the first
alert shows "bar: undefined" and the second "bar: function".

How would I be able to do this? I have read endless tutorials, but I
can't seem to find the answer. Perhaps the answer is obvious, and I
just haven't realised?

There are a number of methods, which is "best" depends on the
situation. If this is for a utility function, the simplest way is to
create a namespace object then add everything as a property of that
object:

var tableUtilities = {};

tableUtilities.makeTable = function(param1, param2)
{
/* function body */
alert('makeTable: ' + param1 + ' ' + param2);
}

tableUtilities.makeTable.makeRow = function(param1, param2)
{
/* function body */
alert('makeTable.makeRow: ' + param1 + ' ' + param2);
}

tableUtilities.makeTable('firstParam', 'secondParam');
tableUtilities.makeTable.makeRow('firstParam', 'secondParam');
Of course in this context, it doesn't make much sense for makeRow to be
a property of makeTable. You may like to read the following articles:

Douglas Crockford - Private Members in JavaScript
<URL: http://www.crockford.com/javascript/private.html >

Richard Cornford - Private Static Members in Javascript
<URL: http://www.litotes.demon.co.uk/js_in...te_static.html >
--
Rob
Rob,

Thank you so very much for your input. You have also managed to answer
a few other questions which I was originally unsure of the answers, and
would have no doubt ended up asking in the future.

For the record, yes, this was just for learning purposes. As you could
probably tell, the object itself doesn't really serve much of a
purpose, I am simply experimenting with objects and getting a feel as
to how best to create them, which in turn should save me time with
coding and lots of mistakes. far too many times have I had to
completely scrap an object and start over as I went around it the wrong
way, made it too complicated, and in order to uncomplicate it, one
piece of code needed changing, which breaks a lot of other code too.

I had been considering whether or not I should just make one large self
contained object, or start with an empty object and then just add
properties and methods as needed. I had taken an educated guess that
adding methods after declaring the object itself would make it easier
to maintain, and no doubt easier to check for syntax errors but I was
unsure about impact (if any), it would have on the security of that
object with ragards to privilages. I guess that as C++ was one of the
first languages I dabbled in, I am too used to making code that other
people can also use, and therefore fool-proofing it against misuse.
Whereas with JavaScript. Unless someone physically changes the code
(which would be their own fault if something broke), I can ensure it's
made to fit my own needs rather than the needs of other people who want
to come up with many more different uses for the same code. (i.e making
the code more universal).

I am unsure whether it's good practise to code everything this way, or
whether it's just overkill and I am making my life much more difficult
than I need to, or at least as far as JavaScript is concerned.

Thanks again.

Daz.

Dec 22 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by lawrence | last post: by
8 posts views Thread by Andrew Robinson | last post: by
1 post views Thread by Andrew Poulos | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.