473,473 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

create objects in JavaScript

js
Two questions:

1. I created Javascript objects called oNewObj using {} construct as
in the follwoing code segment. When I clicked on the <td> element, I
got runtime error "oNewObj is undefined". When I changed the <td>
onclick event to doThis(this), then I can exam (this) object.
However, I really want to exam the oNewObj. Does anyone know how to
do it? Thanks.

2. I need to add additional things to the oNewObj, how could I append
new object to it so that it would look like: {things:[{code:100,
color:'green'}],[{key:215, color:'red'}]};

<html>
<body>
<script language='javascript'>
<!--
var oNewObj = {things:[{code:1, color:'abc'}]};
var strHTML;

strHTML = "<table><tr>"
strHTML += "<td id='1' onclick='javascript:doThis(oNewObj)'>click
this</td>"
strHTML += "</tr>"
strHTML += "<tr>"
strHTML += "<td id='2' onclick='javascript:doAll(oNewObj)'></td>"
strHTML += "</tr></table>"
DIVresult.innerHTML = strHTML;

function doThis(o)
{
alert(o.things[this.id].code + '\n' + o.things[this.id].color);
}

function doAll()
{
var i;
for (i=0; i<=oNewObj.things.length; i++)
alert(oNewObj.things[i].code + '\n' + oNewObj.things[i].color);
}
//-->
<DIV id='DIVresult'></DIV>
</body>
</html>
Jul 20 '05 #1
2 5357
Quite a few simple problems here, I'll try to be brief:

- First, the </script> end tag is missing ;-)
- The "DIVresult" variable is never properly retrieved from the DOM.
- The "DIVresult" HTML element is not created before it's referenced.
- To avoid maintenance confusion, it's bad to use numeric id's for elements.
- It's unnecessary to use the javascript: pseudo-protocol in event handlers.
- JavaScript arrays start at index 0 (zero).
- Automatic object & array creation syntax isn't supported by all browsers.

As such, the following code should help. Tested in IE 6.0, Mozilla 1.4, and
Opera 7.11...

<html>
<head>
<title>Test</title>
<script language="javascript" type="text/javascript">
<!--
function obj() {
this.things = new Array();
this.add = function(_code, _color) {
function things(_code, _color) {
this.code = _code;
this.color = _color;
}
this.things.push(new things(_code, _color));
}
}

// Global var...
var oNewObj = null;

window.onload = function() {
oNewObj = new obj();
oNewObj.add(0, "abc");
oNewObj.add(100, "green");
oNewObj.add(215, "red");

var strHTML = "<table><tr>";
strHTML += "<td onclick=\"doThis(0, oNewObj)\">view first</td>";
strHTML += "</tr>";
strHTML += "<tr>";
strHTML += "<td onclick=\"doAll(oNewObj)\">view all</td>";
strHTML += "</tr></table>";

if (typeof(document.getElementById) == "undefined") return;
var DIVresult = document.getElementById("DIVresult");
if (DIVresult) DIVresult.innerHTML = strHTML;
}

function doThis(i, o) {
alert(o.things[i].code + "\n" + o.things[i].color);
}

function doAll() {
for (var i = 0; i < oNewObj.things.length; i++)
alert(oNewObj.things[i].code + "\n" + oNewObj.things[i].color);
}
// -->
</script>
</head>
<body>
<DIV id="DIVresult"></DIV>
</body>
</html>

L8R

"js" <an********@yahoo.com> wrote in message
news:23**************************@posting.google.c om...
| Two questions:
|
| 1. I created Javascript objects called oNewObj using {} construct as
| in the follwoing code segment. When I clicked on the <td> element, I
| got runtime error "oNewObj is undefined". When I changed the <td>
| onclick event to doThis(this), then I can exam (this) object.
| However, I really want to exam the oNewObj. Does anyone know how to
| do it? Thanks.
|
| 2. I need to add additional things to the oNewObj, how could I append
| new object to it so that it would look like: {things:[{code:100,
| color:'green'}],[{key:215, color:'red'}]};
|
| <html>
| <body>
| <script language='javascript'>
| <!--
| var oNewObj = {things:[{code:1, color:'abc'}]};
| var strHTML;
|
| strHTML = "<table><tr>"
| strHTML += "<td id='1' onclick='javascript:doThis(oNewObj)'>click
| this</td>"
| strHTML += "</tr>"
| strHTML += "<tr>"
| strHTML += "<td id='2' onclick='javascript:doAll(oNewObj)'></td>"
| strHTML += "</tr></table>"
| DIVresult.innerHTML = strHTML;
|
| function doThis(o)
| {
| alert(o.things[this.id].code + '\n' + o.things[this.id].color);
| }
|
| function doAll()
| {
| var i;
| for (i=0; i<=oNewObj.things.length; i++)
| alert(oNewObj.things[i].code + '\n' + oNewObj.things[i].color);
| }
| //-->
| <DIV id='DIVresult'></DIV>
| </body>
| </html>
Jul 20 '05 #2
j s
Thank you very much for the detailed response.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3

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

Similar topics

9
by: relaxedrob | last post by:
Howdy All! I am still getting my head around a few base concepts. Any comments or criticisms on the below definitions would be most welcome! A function is an object. JavaScript objects have...
8
by: Steve Neill | last post by:
Can anyone suggest how to create an arbitrary object at runtime WITHOUT using the deprecated eval() function. The eval() method works ok (see below), but is not ideal. function Client() { }...
2
by: sonu | last post by:
I am tring to create activexobject like var Fo =new ActiveXObject("Scripting.FileSystemObject"); but i am finding error that automation server cant create object any help
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
14
by: hgraham | last post by:
Hi, I'm trying to understand how to work the dom, and all I'm trying to do is insert a link right before another link in the html based on it's href value. This isn't a real world example - I'm...
13
by: Andy Baxter | last post by:
Can anyone recommend a good online guide to using objects in javascript? The book I bought (DHTML Utopia) suggests using objects to keep the code clean and stop namespace clashes between different...
3
pbmods
by: pbmods | last post by:
AN INTRODUCTION TO FUNCTION OBJECTS LEVEL: INTERMEDIATE PREREQS: OBJECTS You've seen it before. You're setting up an XMLHttpRequest call, and you need to execute a function when it returns, so...
13
by: paragpdoke | last post by:
Hello Everyone. Merry Christmas to all ! I'm a JavaScript newbie (and new to thescripts.com too). And I have problems in working with objects in JavaScript. The problem: <a...
6
by: The Natural Philosopher | last post by:
I am trying to create what amounts to an array of 'structures'. I.e. I want to dynamically add to and access an object as myobject.anothernumber // actually represents a nesting level. and ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
1
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...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.