LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   inheritance in javascript (https://www.linuxquestions.org/questions/programming-9/inheritance-in-javascript-830042/)

entz 09-02-2010 06:56 PM

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...

Sergei Steshenko 09-02-2010 07:15 PM

What is exactly the problem ?

entz 09-02-2010 07:40 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4086517)
What is exactly the problem ?

the problem is , how to inherit class circle from class point.

i thought the question was obvious..

cheers

Sergei Steshenko 09-02-2010 07:52 PM

Quote:

Originally Posted by entz (Post 4086530)
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 ?

Sergei Steshenko 09-02-2010 08:00 PM

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

entz 09-02-2010 08:03 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4086537)
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

entz 09-02-2010 08:06 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4086541)
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

Sergei Steshenko 09-02-2010 08:50 PM

Quote:

Originally Posted by entz (Post 4086545)
...
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.

entz 09-02-2010 09:01 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4086575)
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..

Sergei Steshenko 09-02-2010 09:07 PM

Quote:

Originally Posted by entz (Post 4086587)
...
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.

Sergei Steshenko 09-02-2010 09:08 PM

Quote:

Originally Posted by entz (Post 4086587)
table look ups ? ... what are you talking about ?

How do you call function in an interpreted language ?

entz 09-02-2010 09:16 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4086589)
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

Sergei Steshenko 09-02-2010 09:22 PM

Quote:

Originally Posted by entz (Post 4086593)
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.

Sergei Steshenko 09-02-2010 09:30 PM

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.

ghostdog74 09-02-2010 09:36 PM

Quote:

Originally Posted by entz (Post 4086506)
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.


All times are GMT -5. The time now is 12:20 AM.