LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-02-2010, 06:56 PM   #1
entz
Member
 
Registered: Mar 2007
Location: Milky Way , Planet Earth!
Distribution: Opensuse
Posts: 453
Blog Entries: 3

Rep: Reputation: 40
inheritance in javascript


hello,

this goes out to all js programmers out there,

so it seems from the looks of things that js is so dynamic right (at least at the moment) that its authors at EMCA have left the inheritance issue open. (to both the advantage and confusion or even dismay of developers)

okay , enough of the small talk
does anybody know how inheritance goes in js?

here is some code that you may take as a benchmark (THIS CODE IS IMPROVISED)
Code:
function point(){   // class point
   this.set_position = function(x,y){
       this.pos_x = x;
       this.pos_y = y;
   }
   this.get_position = function(){
       pos = new Array;
       pos[x] = this.pos_x;
       pox[y] = this.pos_y;
       return pos;
   }
}

function circle(){ // class circle
   // ATTENTION WE NEED inheritance from class point here !!!
   this.set_radias = function(r){
      this.radius = r;
   }
   this.get_circumference = function(){
      return this.radius * PI * 2;
   }
}

// Okay , it's Instantiation Time !!

var omega = new circle();
omega.set_position(34,-1);
omega.set_radius(5);
alert("Omega Coordinates: " + omega.get_position());
alert("Omega Circumference: " + omega.get_circumference());

//you get the deal ....
Input would be appreciated...

Last edited by entz; 09-02-2010 at 06:57 PM.
 
Old 09-02-2010, 07:15 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
What is exactly the problem ?
 
Old 09-02-2010, 07:40 PM   #3
entz
Member
 
Registered: Mar 2007
Location: Milky Way , Planet Earth!
Distribution: Opensuse
Posts: 453

Original Poster
Blog Entries: 3

Rep: Reputation: 40
Quote:
Originally Posted by Sergei Steshenko View Post
What is exactly the problem ?
the problem is , how to inherit class circle from class point.

i thought the question was obvious..

cheers
 
Old 09-02-2010, 07:52 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by entz View Post
the problem is , how to inherit class circle from class point.

i thought the question was obvious..

cheers
No, the question is not obvious - it is not clear why inheritance from 'point' is needed in the first place.

And are functions first class objects in JavaScript ?
 
Old 09-02-2010, 08:00 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Anyway, can't you simply call 'point' constructor inside 'circle' constructor ? And then implement 'circle' methods by simply calling 'point' instance methods ?

Last edited by Sergei Steshenko; 09-02-2010 at 08:10 PM.
 
Old 09-02-2010, 08:03 PM   #6
entz
Member
 
Registered: Mar 2007
Location: Milky Way , Planet Earth!
Distribution: Opensuse
Posts: 453

Original Poster
Blog Entries: 3

Rep: Reputation: 40
Quote:
Originally Posted by Sergei Steshenko View Post
No, the question is not obvious - it is not clear why inheritance from 'point' is needed in the first place
well ,i hate to be rude but that's a common example used to demonstrate how object oriented programming works , DOH !

for starters the function set_position and get_position used in point are eventually gonna be the same ones used for circle since every circle has a point (it's called the CENTER of circle)

Quote:
And are functions first class objects in JavaScript ?
I'm afraid , now you've to explain to me what you're trying to ask..
all i can say is that , what you've seen is the common syntax used to declare js classes and their methods and variables.

btw , it looks from the signature of yours that you're more knowledgable in python than in js , you don't have to solve this problem if this is outside of realm of expertize...

cheers
 
Old 09-02-2010, 08:06 PM   #7
entz
Member
 
Registered: Mar 2007
Location: Milky Way , Planet Earth!
Distribution: Opensuse
Posts: 453

Original Poster
Blog Entries: 3

Rep: Reputation: 40
Quote:
Originally Posted by Sergei Steshenko View Post
Anyway, can't you simply call 'point' constructor inside 'circle' constructor ? And then implemented 'circle' methods by simple calling 'point' instance methods ?
well while you're posting , i've updated the code

Code:
// inheritance exercise

function point(){   // class point
   this.set_position = function(x,y){
       this.pos_x = x;
       this.pos_y = y;
   }
   this.get_position = function(){
       pos = new Array;
       pos['x'] = this.pos_x;
       pos['y'] = this.pos_y;
       return pos;
   }
}

function circle(){ // class circle
   var p = new point(); // sugar needed
   this.set_position = p.set_position; // sugar needed
   this.get_position = p.get_position; // sugar needed
   // ATTENTION WE NEED inheritance from class point here !!!
   this.PI = 3.14;
   this.set_radius = function(r){
      this.radius = r;
   }
   this.get_circumference = function(){
      return this.radius * this.PI * 2;
   }
}

// Okay , it's Instantiation Time !!

var omega = new circle();
omega.set_position(34,-1);
omega.set_radius(5);
tmp = omega.get_position();
x = tmp['x'];
y = tmp['y'];
alert("Omega Coordinates: " + x + " | " + y);
alert("Omega Circumference: " + omega.get_circumference());

//you get the deal ....
See where i've commented "sugar needed"

i need a technique in js that automatically does this operation without having to "inherit" every single function manually

in other words i need syntactic sugar !

cheers

Last edited by entz; 09-02-2010 at 08:08 PM.
 
Old 09-02-2010, 08:50 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by entz View Post
...
i need a technique in js that automatically does this operation without having to "inherit" every single function manually

in other words i need syntactic sugar !

cheers
Back to my question about the need of inheritance - if I understrand correctly, in order to access set position methods in your 'circle' instance you need two table lookups - first in 'circle' instance and then in it's 'point' instance - unless JavaScript compiler is optimizing.

Regarding the need of syntactic sugar - write or use a text preprocessor.
 
Old 09-02-2010, 09:01 PM   #9
entz
Member
 
Registered: Mar 2007
Location: Milky Way , Planet Earth!
Distribution: Opensuse
Posts: 453

Original Poster
Blog Entries: 3

Rep: Reputation: 40
Quote:
Originally Posted by Sergei Steshenko View Post
Back to my question about the need of inheritance - if I understrand correctly, in order to access set position methods in your 'circle' instance you need two table lookups - first in 'circle' instance and then in it's 'point' instance - unless JavaScript compiler is optimizing.

Regarding the need of syntactic sugar - write or use a text preprocessor.
table look ups ? preprocessor ? what are you talking about ?

Javascript is an INTERPRETED language there is no pre-processing !

the issue is HOW to do inheritance in js not to explain why , that's beyond the scope this thread..

Last edited by entz; 09-02-2010 at 09:03 PM.
 
Old 09-02-2010, 09:07 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by entz View Post
...
Javascript is an INTERPRETED language there is no pre-processing !
...
This is you who are writing the code and this is you who chooses what tools to use. You can use a preprocessor.

In fact, I have my own preprocessor written in Perl and sometimes I use it to produce Perl code.
 
Old 09-02-2010, 09:08 PM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by entz View Post
table look ups ? ... what are you talking about ?
How do you call function in an interpreted language ?
 
Old 09-02-2010, 09:16 PM   #12
entz
Member
 
Registered: Mar 2007
Location: Milky Way , Planet Earth!
Distribution: Opensuse
Posts: 453

Original Poster
Blog Entries: 3

Rep: Reputation: 40
Quote:
Originally Posted by Sergei Steshenko View Post
This is you who are writing the code and this is you who chooses what tools to use. You can use a preprocessor.

In fact, I have my own preprocessor written in Perl and sometimes I use it to produce Perl code.
I KNOW what you mean !

you're suggesting to use a server side language such as php , perl ..etc to write the necessary javascript in realtime before being submitted to the browser....

well what you've described is what we web developers refer to as a "browser hack" (remember this term)

and it's NOT an option !

because :
1) it shifts the load back on the server which is what client side scripting languages such as js are supposed to solve.
2) it complicates things and makes debugging js code a mess

Quote:
How do you call function in an interpreted language ?
the same way i've done it in the previous code section...

cheers
 
Old 09-02-2010, 09:22 PM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by entz View Post
I KNOW what you mean !

you're suggesting to use a server side language such as php , perl ..etc to write the necessary javascript in realtime before being submitted to the browser....

well what you've described is what we web developers refer to as a "browser hack" (remember this term)

and it's NOT an option !

because :
1) it shifts the load back on the server which is what client side scripting languages such as js are supposed to solve.
2) it complicates things and makes debugging js code a mess


the same way i've done it in the previous code section...

cheers
No, I didn't mean that. You write JavaScript once on your machine, and during that session you use whatever tools you like. So no additional load on server whatsoever.

And

Quote:
the same way i've done it in the previous code section.
is not an answer, i.e. it doesn't describe the mechanism.
 
Old 09-02-2010, 09:30 PM   #14
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
http://www.brighthub.com/computing/l...les/19626.aspx :

Quote:
The introduction of a bytecode interpreter. JavaScriptCore currently executes a syntax tree.
- so, the smaller amount of table lookups, the better.
 
Old 09-02-2010, 09:36 PM   #15
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by entz View Post
Input would be appreciated...
The guy is not going to solve your "problem" for you, not at least directly, if not at all. You might want to post your question to stackoverflow.com. There are more experts there on Javascript who can answer your question.

Last edited by ghostdog74; 09-02-2010 at 09:38 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ubuntu Edgy/Firefox 2/Javascript - Firefox closes accessing websites with Javascript Interdictor Ubuntu 8 11-02-2006 11:58 AM
Inheritance question kornerr Programming 6 08-24-2006 07:38 AM
Template inheritance allomeen Programming 7 01-26-2006 03:08 PM
file inheritance tulip4heaven Linux - Networking 1 02-24-2005 02:30 AM
inheritance kalleanka Programming 4 02-29-2004 07:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration