473,772 Members | 3,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(numbe r_of_rows) {
var table = document.create Element('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.appendChi ld(new tableRow);
}
return table;

function tableRow()
{
var tr = document.create Element('tr');
var td1 = document.create Element('td');
var td2 = document.create Element('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.a ppendChild(newT able);
</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.table Row();

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 1512
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(numbe r_of_rows) {
var table = document.create Element('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.appendChi ld(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_cel ls; 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 "constructo rs" 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.table Row();
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('makeTabl e: ' + param1 + ' ' + param2);
}

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

tableUtilities. makeTable('firs tParam', 'secondParam');
tableUtilities. makeTable.makeR ow('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(numbe r_of_rows) {
var table = document.create Element('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.appendChi ld(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_cel ls; 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 "constructo rs" 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.table Row();

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('makeTabl e: ' + param1 + ' ' + param2);
}

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

tableUtilities. makeTable('firs tParam', 'secondParam');
tableUtilities. makeTable.makeR ow('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
2462
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 happens if one of those objects, when it is created, takes a reference to the object that contains it? Do bad things happen? class McShow {
6
2242
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
3378
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 (SqlCommand cm = new SqlCommand("ItemCount", cn)) { cm.CommandType = CommandType.StoredProcedure;
1
1761
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 = "{foo:{bar:1}}"; eval(str); alert(foo.bar) // gives undefined
3
1948
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 using the <object> tag ?? Thanks!
0
1403
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 Net::IMAP::Simple::SSL libraries: 1) The method new returns a valid connection even on 5th try, though the login fails as it looks like IMAP->login somehow knows that 5th object is not valid..Is it possible ? 2) Somtime, the script fails at the...
4
2761
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
9620
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10104
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9912
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.