473,408 Members | 1,775 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,408 software developers and data experts.

JavaScript newbie: Need help in working with objects

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:

[HTML]<a href="javascript:functionName(object);">Click me</a>[/HTML]
Somehow it calls toString for the object and rather than it being processed as an object, it is processed as a string.

Details:
I'm trying to build a documentation UI (using VBA plugin mztools) and the objects I work with are Project, File, Procedure in that order. The Procedure in turn has arrays of Comments and Parameters.

Expand|Select|Wrap|Line Numbers
  1. function File(Name,Files) {
  2.     this.Name=Name;
  3.     this.Files=Files
  4. }
  5. function File(some arguments....,Procedures) {
  6.     // The usual this statements...
  7. }
Some similar declarations for Procedure, and other objects.
I'm able to build a library (not sure if the term is correct) of objects where the Projects array is the parent / reference array I keep referring to.
Now, dynamically, the href of anchors is modified. What I want it to have is:
javascript:functionName(object)
But what gets called is:
javascript:functionName("[object Object]")

I tried using typeOf within "functionName" ...but couldn't figure out what to do with it. Also, I cannot re-construct the object (say Procedure) again from the toString output for it, since there are many more things about it which functionName doesn't know about.

What should my approach be in such a case ? Thank you in advance for all your inputs.
Dec 27 '07 #1
13 1655
gits
5,390 Expert Mod 4TB
hi ...

welcome to theScripts ....

i'm not quite sure whether i could follow your explainations on what you really want to do ... but i think you want to create objects from a list of declared ones and create them from their names? something like the following?

Expand|Select|Wrap|Line Numbers
  1. function MY_OBJ() {
  2.     this.foo = 'bar';
  3. }
  4.  
  5. function create_obj(class_name) {
  6.     return new Function('return new ' + class_name)();
  7. }
  8.  
  9. var a = create_obj('MY_OBJ');
  10.  
kind regards
Dec 27 '07 #2
hi ...

welcome to theScripts ....

i'm not quite sure whether i could follow your explainations on what you really want to do ... but i think you want to create objects from a list of declared ones and create them from their names? something like the following?

Expand|Select|Wrap|Line Numbers
  1. function MY_OBJ() {
  2.     this.foo = 'bar';
  3. }
  4.  
  5. function create_obj(class_name) {
  6.     return new Function('return new ' + class_name)();
  7. }
  8.  
  9. var a = create_obj('MY_OBJ');
  10.  
kind regards
Hello Gits.
Thank you for the welcome note.
Yes...I have been able to build such objects. Next I create a function / procedure called "display_obj". This function expects one argument: reference to an object. Thus my function becomes (trying the CODE tags for the first time):
Expand|Select|Wrap|Line Numbers
  1. function display_obj(objToDisplay) {
  2.     //Some innerHTML modified here where property "foo" is handled.
  3. }
  4.  
This function is called by clicking on an anchor. So the href for the anchor would be:
Expand|Select|Wrap|Line Numbers
  1. javascript:display_obj(a);
  2.  
But, the browser (Fx and IE) treated "a" as "[Object object]" rather than something that had its own property foo.

I hope to have clarified the problem this time. Pardon me for lack of ability to put it in a clear manner.
Dec 27 '07 #3
Hello Gits.
Thank you for the welcome note.
Yes...I have been able to build such objects. Next I create a function / procedure called "display_obj". This function expects one argument: reference to an object. Thus my function becomes (trying the CODE tags for the first time):
Expand|Select|Wrap|Line Numbers
  1. function display_obj(objToDisplay) {
  2.     //Some innerHTML modified here where property "foo" is handled.
  3. }
  4.  
This function is called by clicking on an anchor. So the href for the anchor would be:
Expand|Select|Wrap|Line Numbers
  1. javascript:display_obj(a);
  2.  
But, the browser (Fx and IE) treated "a" as "[Object object]" rather than something that had its own property foo.

I hope to have clarified the problem this time. Pardon me for lack of ability to put it in a clear manner.
Please give me a few minutes to upload a sample html file.
Dec 27 '07 #4
Please give me a few minutes to upload a sample html file.
I have uploaded 3 htm files in attachment: http://paragpdoke.googlepages.com/Documents.zip
[Sorry...couldn't find the upload attachment link on this forum yet. Will try to look for it soon.]

The 3rd one is exactly the problem I'm running into.
Dec 27 '07 #5
I have uploaded 3 htm files in attachment: http://paragpdoke.googlepages.com/Documents.zip
[Sorry...couldn't find the upload attachment link on this forum yet. Will try to look for it soon.]

The 3rd one is exactly the problem I'm running into.
Sorry for re-post. By 3rd I meant file "HrefModification.htm".
Dec 27 '07 #6
gits
5,390 Expert Mod 4TB
you want to display the object's codeview? ... so in FF/Moz there is a simple way with the toSource()-method ... or you may use the print_r() function described in this linked howto ...

kind regards
Dec 27 '07 #7
acoder
16,027 Expert Mod 8TB
[Sorry...couldn't find the upload attachment link on this forum yet. Will try to look for it soon.]
It's not available when you first post, but if you post and then edit within one hour, you should be able to attach the file.
Dec 27 '07 #8
Hello Gits.
Apologies for a delayed response. I tried the toSource method (after your last reply)...but that too did not work. Here's what the modified file looks like:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>
  3.         <script language="JavaScript">
  4.             function obj(foo) {
  5.                 this.foo=foo;
  6.             }
  7.             var a = new obj('thescripts');
  8.             function display_obj(objToDisplay) {
  9.                 document.getElementById('myDiv').innerHTML+=objToDisplay.foo;
  10.             }
  11.             function change_link() {
  12.                 document.getElementById('myLink').href="javascript:display_obj("+a+");";
  13.             }
  14.         </script>
  15.     </head>
  16.     <body>
  17.         <a href="javascript:change_link();">Click me</a><br>
  18.         <a href="javascript:display_obj("+a.toSource()+");" id="myLink">My link will change</a><br>
  19.         <div id="myDiv"></div>
  20.     </body>
  21. </html>
  22.  
Unfortunately, I did not understand the intent of the print_r function. Haven't used PHP yet....so it was a little difficult for a newbie to understand.

All I'm looking for is a way to make the browser display "thescripts" in the div using JavaScript object "a". When the object is passed statically (not sure if the term is correct - all I mean is without modifying the href via JavaScript) it works. However, when the href is modified by the script, it fails to treat the object as an object.

Thank you acoder for the information about attachments. I will remember this when I have to upload something next time.

Thanks in advance for any more directions.
Dec 29 '07 #9
gits
5,390 Expert Mod 4TB
hi ...

is that what you want? use the onclick instead of the href:

[HTML]<html>
<head>
<script language="JavaScript">
function obj(foo) {
this.foo = foo;
}

var a = new obj('thescripts');

function display_obj(objToDisplay) {
alert(objToDisplay.toSource())
document.getElementById('myDiv').innerHTML = objToDisplay.foo;
}

function change_link() {
document.getElementById('myLink').onclick = "display_obj(" + a + ");";
}
</script>
</head>
<body>
<a href="#" onclick="change_link();">Click me</a><br>
<a href="#" onclick="display_obj(a);" id="myLink">My link will change</a><br>
<div id="myDiv"></div>
</body>
</html>
[/HTML]
kind regards
Dec 29 '07 #10
hi ...

is that what you want? use the onclick instead of the href:

[HTML]<html>
<head>
<script language="JavaScript">
function obj(foo) {
this.foo = foo;
}

var a = new obj('thescripts');

function display_obj(objToDisplay) {
alert(objToDisplay.toSource())
document.getElementById('myDiv').innerHTML = objToDisplay.foo;
}

function change_link() {
document.getElementById('myLink').onclick = "display_obj(" + a + ");";
}
</script>
</head>
<body>
<a href="#" onclick="change_link();">Click me</a><br>
<a href="#" onclick="display_obj(a);" id="myLink">My link will change</a><br>
<div id="myDiv"></div>
</body>
</html>
[/HTML]
kind regards
Hi again Gits.
Ummm...no that is not going to help too. See, in the same example you shared above, having:
Expand|Select|Wrap|Line Numbers
  1. <a href="#" onclick="display_obj(a);" id="myLink">My link will change</a><br>
  2.  
is equivalent of:
Expand|Select|Wrap|Line Numbers
  1. <a href="javascript:display_obj(a);" id="myLink">My link will change</a><br>
  2.  
Both these methods are directly referring to the object "a" directly. Meaning, JavaScript is not modifying either the "href" or the "onclick" attributes of the 2nd anchor in the DOM. Now let us turn to:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('myLink').onclick = "display_obj(" + a + ");";
  2.  
or...as in my example:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('myLink').href = "display_obj(" + a + ");";
  2.  
Both these lines do not work as I want them to. They somehow fail to treat "a" as an object. My file currently has the following (and does not work):
[HTML]
<html>
<head>
<script language="JavaScript">
function obj(foo) {
this.foo = foo;
}
var a = new obj('thescripts');
function display_obj(objToDisplay) {
alert(objToDisplay.toSource())
document.getElementById('myDiv').innerHTML = objToDisplay.foo;
}
function change_link() {
document.getElementById('myLink').onclick = "display_obj(" + a + ");";
}
</script>
</head>
<body>
<a href="#" onclick="change_link();">Click me</a><br>
<a href="#" id="myLink">My onclick will change</a><br>
<div id="myDiv"></div>
</body>
</html>
[/HTML]
All I want to happen is that JavaScript treats "a" as an object after the "href" or "onclick" have been modified by JavaScript.

(I now know the problem with my delayed responses. Even if I've set "Instant email notification" in my control panel, I receive no updates over mail. It is necessary to monitor the thread to learn about replies.)

Thanks in advance again,
Parag P. Doke
Jan 2 '08 #11
gits
5,390 Expert Mod 4TB
sorry my bad :) ... now have a look ... we must assign a function reference to the onclick-handler ... so it works now. we replace the onclick through calling change_link() ...

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>
  3.     <script language="JavaScript">
  4.     function obj(foo) {
  5.         this.foo = foo;
  6.     }
  7.  
  8.     var a = new obj('thescripts');
  9.  
  10.     function display_obj(objToDisplay) {
  11.         alert(objToDisplay.toSource())
  12.         document.getElementById('myDiv').innerHTML = objToDisplay.foo;
  13.     }
  14.  
  15.     function change_link(obj) {
  16.         document.getElementById('myLink').onclick = function() {
  17.             return display_obj(obj);
  18.         }
  19.     }
  20.     </script>
  21.     </head>
  22.     <body>
  23.         <a href="#" onclick="change_link(a);">Click me</a><br>
  24.         <a href="#" onclick="alert('test');" id="myLink">My link will change</a><br>
  25.         <div id="myDiv"></div>
  26.     </body>
  27. </html>
  28.  
kind regards
Jan 2 '08 #12
sorry my bad :) ... now have a look ... we must assign a function reference to the onclick-handler ... so it works now. we replace the onclick through calling change_link() ...

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>
  3.     <script language="JavaScript">
  4.     function obj(foo) {
  5.         this.foo = foo;
  6.     }
  7.  
  8.     var a = new obj('thescripts');
  9.  
  10.     function display_obj(objToDisplay) {
  11.         alert(objToDisplay.toSource())
  12.         document.getElementById('myDiv').innerHTML = objToDisplay.foo;
  13.     }
  14.  
  15.     function change_link(obj) {
  16.         document.getElementById('myLink').onclick = function() {
  17.             return display_obj(obj);
  18.         }
  19.     }
  20.     </script>
  21.     </head>
  22.     <body>
  23.         <a href="#" onclick="change_link(a);">Click me</a><br>
  24.         <a href="#" onclick="alert('test');" id="myLink">My link will change</a><br>
  25.         <div id="myDiv"></div>
  26.     </body>
  27. </html>
  28.  
kind regards
PERFECT ! That is exactly what I wanted to happen.
Though I do not understand the significance of each step, I'll try modify this code step by step and learn. Things I learnt from this topic till now:
1) "onclick" is better at working with objects rather than "href". (Please correct me if I'm making mistakes.)
2) One can skip "javascript:" for events as against standard attributes like "href" (shorter code).
3) If you accept the name of an object instance as string ("a"), for it to be treated as an object again, one should use a function reference. Somewhere when passing the string to the reference, it gets transformed back into the object.

Thank you so much gits for your patience. I'm not even sure if I used the right terms for all these questions :-).
Jan 2 '08 #13
gits
5,390 Expert Mod 4TB
PERFECT ! That is exactly what I wanted to happen.
Though I do not understand the significance of each step, I'll try modify this code step by step and learn. Things I learnt from this topic till now:
1) "onclick" is better at working with objects rather than "href". (Please correct me if I'm making mistakes.)
2) One can skip "javascript:" for events as against standard attributes like "href" (shorter code).
3) If you accept the name of an object instance as string ("a"), for it to be treated as an object again, one should use a function reference. Somewhere when passing the string to the reference, it gets transformed back into the object.

Thank you so much gits for your patience. I'm not even sure if I used the right terms for all these questions :-).
no problem ... :) i'm glad to help you ... to you points:

1. that's really true :)
2. that's really true either :)
3. not quite sure what you mean with that. the code above works as follows: we reassign the click-handler and that handler has to be a function-reference so that it is called later on when the event gets fired. now in our special case we have to pass a reference to the object too ... since the handler should access the object. let me give you a short example what a function reference is:

Expand|Select|Wrap|Line Numbers
  1. var my_func = function(param) {
  2.     // function code
  3. };
  4.  
  5. // as you can see we may assign a variable name to
  6. // a function, we could call it with:
  7.  
  8. my_func('foo');
  9.  
  10. // this is equivalent to:
  11.  
  12. function my_func(param) {
  13.     // function code
  14. };
  15.  
  16. // so the name of the function is its reference and we could use 
  17. // the following for example:
  18.  
  19. window.setTimeout(my_func, 1000);
  20.  
  21. // or even - here func_with_param acts as a closure, that means
  22. // it is called later on with a param that we store in the function for
  23. // later use
  24.  
  25. var func_with_param = function() {
  26.     return my_func('foo');
  27. }
  28.  
  29. window.setTimeout(func_with_param, 1000);
  30.  
  31. // this way we avoid the ugly but typical eval-like-usage:
  32.  
  33. window.setTimeout('my_func("bar");', 1000);
  34.  
kind regards
Jan 2 '08 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
7
by: Kevin Newman | last post by:
I've been toying with a namespace manager, and wanted to get some input. So what do you think? if (typeof com == 'undefined') var com = {}; if (!com.unFocus) com.unFocus = {}; ...
8
by: chrisdude911 | last post by:
how do i add video into a javascript web page with my own custom buttons?
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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
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,...
0
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...

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.