473,387 Members | 3,033 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,387 software developers and data experts.

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
2 1481
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: lawrence | last post by:
How dangerous or stupid is it for an object to have a reference to the object which contains it? If I have a class called $controllerForAll which has an arrray of all the objects that exist, what...
6
by: Rufnex | last post by:
Hi, is there a delete for a object inside the constructor, while i init it? i will try something like that: var obj = function(a) { if (!a) delete this; this.a = a; }
8
by: Andrew Robinson | last post by:
Are these two equivalent? Is one better than the other? I tend to go with #1 but started wondering.... Thanks, 1: using (SqlConnection cn = new SqlConnection(DataConnection)) using...
1
by: Andrew Poulos | last post by:
There's an object: foo = new Object(); and a property called 'bar' with a value of 1 is to be added to it using JSON. If I try the following it only demonstrates my ignorance: var str =...
3
by: skyy | last post by:
Hi... Is it possible to insert C++ object into HTML page using the <object> tag? If cant, then i guess the only way is to convert the C code to active X control then insert the active X object...
0
by: contactme | last post by:
Hi, Is it possible to open concurrent connections using Net::IMAP::Simple library ? My IMAP server allows 4 connections per ip, so I am having following problems while using Net::IMAP::Simple and...
4
by: backups2007 | last post by:
is it possible to use an include function inside an isset function? something like: if(isset($_POST)) { include("insert.php"); }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.