473,748 Members | 6,037 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JavaScript newbie: Need help in working with objects

62 New Member
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="JavaScrip t: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:func tionName(object )
But what gets called is:
javascript:func tionName("[object Object]")

I tried using typeOf within "functionNa me" ...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 1692
gits
5,390 Recognized Expert Moderator Expert
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
paragpdoke
62 New Member
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_ob j". 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
paragpdoke
62 New Member
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_ob j". 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
paragpdoke
62 New Member
Please give me a few minutes to upload a sample html file.
I have uploaded 3 htm files in attachment: http://paragpdoke.goog lepages.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
paragpdoke
62 New Member
I have uploaded 3 htm files in attachment: http://paragpdoke.goog lepages.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 "HrefModificati on.htm".
Dec 27 '07 #6
gits
5,390 Recognized Expert Moderator Expert
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 Recognized Expert Moderator MVP
[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
paragpdoke
62 New Member
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 Recognized Expert Moderator Expert
hi ...

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

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

var a = new obj('thescripts ');

function display_obj(obj ToDisplay) {
alert(objToDisp lay.toSource())
document.getEle mentById('myDiv ').innerHTML = objToDisplay.fo o;
}

function change_link() {
document.getEle mentById('myLin k').onclick = "display_ob j(" + a + ");";
}
</script>
</head>
<body>
<a href="#" onclick="change _link();">Click me</a><br>
<a href="#" onclick="displa y_obj(a);" id="myLink">My link will change</a><br>
<div id="myDiv"></div>
</body>
</html>
[/HTML]
kind regards
Dec 29 '07 #10

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

Similar topics

22
4639
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 month I had enough of extra proof that the cat doesn't hount mice anymore in more and more situations. And the surrent sicretisme among array and hash is the base for it. I summarized all points in this article:...
7
7439
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 = {}; com.unFocus.Namespaces = new function() { this.register = function(namespace) { namespace = namespace.split('.');
8
3674
by: chrisdude911 | last post by:
how do i add video into a javascript web page with my own custom buttons?
24
6341
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 have on his site, a Javascript Calculator for working out the cost of what they want, for example: 1 widget and 2 widglets = £5.00
0
8991
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
8830
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9544
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
9372
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
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
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
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3313
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 we have to send another system
2
2783
muto222
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.