By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,084 Members | 1,956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,084 IT Pros & Developers. It's quick & easy.

What is an Object Graph and how can it be implemented in my JavaScript code

P: 74
Hello Everyone,
I am working on a creating an instance of a class in JavaScript using Object-Oriented skills. The question I have is how can I implement an object graph with using the code that I have so far in my JavaScript file? Here is my code so far.
Expand|Select|Wrap|Line Numbers
  1.  function Person(Name, Grade, Gender, GPA, ClassRank)
  2.     { 
  3.          this.Name= name;
  4.     **** this.Grade  = grade;
  5.     **** this.Gender = gender;
  6.     **** this.GPA = gpa;
  7.     ***  this.ClassRank = classRank;
  8.     }
  9.     var student1= new Person("Gray", 'B', 'F', 3.34, 5);
  10.     var student2= new Person("Jerry", 'A', 'M', 3.99, 4); 
  11.  
  12.     document.getElementById("demo1").innerHTML= student1.Name + " " + student1.Grade + "* " + student1.Gender + *" "* + student1.GPA + " " + student1.classRank; 
  13.  
  14.   document.getElementById("demo2").innerHTML= student2.Name + " " + student2.Grade + "* " + student2.Gender + *" "* + student2.GPA + " " + student2.classRank; 
  15.  
  16.  
Expand|Select|Wrap|Line Numbers
  1.  <html> 
  2.     <head>
  3.     <title></title>
  4.     </head>
  5.     <body>
  6.     <p id= "demo1"> </p>
  7.     <p id= "demo2"></p>
  8.     </body>
  9.     </html>
  10.  
Feb 28 '17 #1
Share this Question
Share on Google+
24 Replies


Dormilich
Expert Mod 5K+
P: 8,639
The question I have is how can I implement an object graph
what do you consider an 'object graph'?
Feb 28 '17 #2

P: 74
@Dormilich,

When you link two different objects together.
Feb 28 '17 #3

Dormilich
Expert Mod 5K+
P: 8,639
and what should that look like, HTML-wise?
Feb 28 '17 #4

P: 74
@Dormilich I guess I could just get rid of the class names and just use the external JS file to show the JavaScript objects in the HTML page. How do I link the objects together though. Object graph is all about making the objects interact with one another. All I am question is this possible to do in JavaScript? How can it be done?
Feb 28 '17 #5

Dormilich
Expert Mod 5K+
P: 8,639
How do I link the objects together though.
the most simple thing were to set one object as a property of the other. But you can use any design pattern you want, if it works for you.
Feb 28 '17 #6

P: 74
@Dormilich How would I do that? Any examples. I just want to master making the objects interact with each other.
Feb 28 '17 #7

Dormilich
Expert Mod 5K+
P: 8,639
Expand|Select|Wrap|Line Numbers
  1. var foo = {
  2.   a: 1
  3. };
  4.  
  5. var bar = {
  6.   b: 2
  7. };
  8.  
  9. bar.f = foo;
  10.  
Feb 28 '17 #8

P: 74
@Dormilich So bar is now equal to foo? This is the structure that I think I was looking for.
Feb 28 '17 #9

Dormilich
Expert Mod 5K+
P: 8,639
So bar is now equal to foo?
bar.f is identical to foo. there would be no point in having foo and bar referencing the same object.
Feb 28 '17 #10

P: 74
@Dormilich This would be a simple example of an object graph? How could I get my two objects to link together?
Feb 28 '17 #11

Dormilich
Expert Mod 5K+
P: 8,639
This would be a simple example of an object graph?
Not as I understand an object graph:

An object graph is a view of an object system at a particular point in time.
Feb 28 '17 #12

P: 74
@Dormilich I guess most people are not that familiar with it in JavaScript. For example,
var object1 = {};
var object2 = {};
object1.next = object2;

These two objects are identical to each other, right? But how can make my objects identical to each other?
Feb 28 '17 #13

Dormilich
Expert Mod 5K+
P: 8,639
These two objects are identical to each other, right?
no .
Feb 28 '17 #14

P: 74
@Dormilich why not? Can you please explain why it's not correct logic?
Feb 28 '17 #15

P: 74
@Dormilich Are they just pointing at each other? If so, how can I get my objects to just point at each other?
Feb 28 '17 #16

Dormilich
Expert Mod 5K+
P: 8,639
Are they just pointing at each other?
object1 has a reference to object2. That is completely different from being identical.
Feb 28 '17 #17

P: 74
@Dormilich How can I get my object 1 to reference to object 2 that I wrote in my above JS file? Is it possible to do it with my objects?
Feb 28 '17 #18

Dormilich
Expert Mod 5K+
P: 8,639
you got the referencing right, but it's not called 'identity'.
Feb 28 '17 #19

P: 74
@Dormilich I am sorry for the long extended conversations, but would it be possible to show me with an example of how I can point the objects to one another?
Feb 28 '17 #20

Dormilich
Expert Mod 5K+
P: 8,639
see post #13
Feb 28 '17 #21

P: 74
@Dormilich I know that was a simple example, but how can it be done using my objects in my JS file. For example with this JavaScript code below:
var*student1=*new*Person("Gray",*'B',*'F',*3.34,*5 );
var*student2=*new*Person("Jerry",*'A',*'M',*3.99,* 4);*
Feb 28 '17 #22

Dormilich
Expert Mod 5K+
P: 8,639
It makes no sense to reference student1 in student2 or vice versa.
Feb 28 '17 #23

P: 74
@Dormilich
Not even point to each other. I know it's a stupid question, but that's how I learn. All questions are never stupid only the ones never asked.
Feb 28 '17 #24

P: 17
Your post #1 code had too many errors.
See what was fixed here and maybe begin your question again (?)
Expand|Select|Wrap|Line Numbers
  1. <html> 
  2.  <head>
  3.   <title></title>
  4.  </head>
  5.  <body>
  6.   <p id= "demo1"> </p>
  7.   <p id= "demo2"></p>
  8.  
  9. <script>
  10. function Person(Name, Grade, Gender, GPA, ClassRank) { 
  11.   this.Name= Name;
  12.   this.Grade  = Grade;
  13.   this.Gender = Gender;
  14.   this.GPA = GPA;
  15.   this.ClassRank = ClassRank;
  16. }
  17. var student1= new Person("Gray", 'B', 'F', 3.34, 5);
  18. var student2= new Person("Jerry", 'A', 'M', 3.99, 4); 
  19.  
  20. document.getElementById("demo1").innerHTML =
  21.  student1.Name + " " + student1.Grade + "* " + student1.Gender + " " + student1.GPA + " " + student1.ClassRank; 
  22. document.getElementById("demo2").innerHTML =
  23.  student2.Name + " " + student2.Grade + "* " + student2.Gender + " " + student2.GPA + " " + student2.ClassRank; 
  24.  
  25. </script>
  26.  
  27.  </body>
  28. </html>
  29.  
What is it that you are really trying to do?
Mar 9 '17 #25

Post your reply

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